Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is Abstract factory pattern with example?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 747
    Comment on it

    We know that factory pattern provides single level abstraction and used to create object easily but in a case of more security, we can use one more level of abstraction by using the abstract factory pattern. we can say that this provides factory of factories.

    Example :

    1. Create interfaces :

    public interface Shape {
        public void draw(String shapeType);
    }
    
    
    public interface Color {
        public void color(String fillColor);
    }

     

    2. Create rectangle and circle type class :

    public class Rectangle implements Shape, Color{
    
        @Override
        public void color(String fillColor) {
            System.out.println("fill color :" + fillColor);
        }
    
        @Override
        public void draw(String shapeType) {
            System.out.println("shape type :" + shapeType);
        }
    
    }
    
    
    public class Circle implements Shape, Color{
    
        @Override
        public void color(String fillColor) {
            System.out.println("fill color :" + fillColor);
        }
    
        @Override
        public void draw(String shapeType) {
            System.out.println("shape type :" + shapeType);
        }
    }

     

    3.Create Abstract Factory class

    public abstract class AbstractFactory {
        abstract Shape getShape(String type);
        abstract Color getColor(String color);
    }


    4. Create Shape factory that 'll define shape :

    public class ShapeFactory extends AbstractFactory{
    
        @Override
        Shape getShape(String type) {
            if (type.equalsIgnoreCase("rectangle")) {
                return new Rectangle();
            }else if (type.equalsIgnoreCase("circle")) {
                return new Circle();
            }
            
            return null;
        }
    
        @Override
        Color getColor(String color) {
            return null;
        }
    
    }
    
    
    and then color factory to define color :
    
    
    public class ColorFactory extends AbstractFactory{
    
        @Override
        Shape getShape(String type) {
            return null;
        }
    
        @Override
        Color getColor(String color) {
            if (color.equalsIgnoreCase("red")) {
                return new Rectangle();
            }else if (color.equalsIgnoreCase("orange")) {
                return new Circle();
            }
            return null;
        }
    
    }

     

    5.Now create factory provider that ll provide type of factory based on given information :

    public class FactoryProvider {
        public static AbstractFactory getFactory(String factoryFor){
            if (factoryFor.equalsIgnoreCase("shape")) {
                return new ShapeFactory();
            }else if (factoryFor.equalsIgnoreCase("color")) {
                return new ColorFactory();
            }
            return null;
        }
    }

     

    6. In Main :

    public class AbstractFactoryDemoClass {
    
        public static void main(String[] args) {
            
            AbstractFactory abstractFactoryShape = FactoryProvider.getFactory("shape");
            
            Shape shapeRectangle = abstractFactoryShape.getShape("rectangle");
            Shape shapeCircle = abstractFactoryShape.getShape("circle");
            shapeRectangle.draw("rectangle");
            shapeCircle.draw("circle");
            
            AbstractFactory abstractFactoryColor = FactoryProvider.getFactory("color");
            Color colorRectangle = abstractFactoryColor.getColor("red");
            Color colorCircle = abstractFactoryColor.getColor("orange");
            colorRectangle.color("red");
            colorCircle.color("orange");
            
        }
    
    }


    You can get Factory using Factory provider by giving Shape type then u ll have Abstract Factory to generate Shape object and color object.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: