Abstract Factory Desion Pattern



This is a creational design pattern. This is actually one more layer abstraction on top of factory method pattern. You can say it factory of factory.
Abstract factory is responsible for creating factory of related objects. Each generated factory gives the objects as per Factory pattern.
Example:
We will have two types of related interfaces Shape and Color. Will have a factory(ShapeFactory) to return the correct shape and another factory(ColourFactory) to return the coloue object.
Now on top of these two factories we will have another Abstract Factory which is responsible to return the required Factory (ShapeFactory or ColourFactory).


//One Interface
public interface Shape {
    public void draw();
}

public class CircelShape implements Shape {
    public void draw() {
        System.out.println("Draw Circel");
    }
}

public class RectangleShape implements Shape {
    public void draw() {
        System.out.println("Draw Rectangle");
    }
}

// Another related interface
public interface Color {
    public void fill();
}

public class GreenColor implements Color {
    public void fill() {
        System.out.println("Fill colour Green");
    }
}

public class RedColor implements Color {
    public void fill() {
        System.out.println("Fill colour Red");
    }
}

public abstract class AbstractFactory {
    public abstract Shape getShape(String shapeType);
    public abstract Color getColor(String colourType);
}

public class ShapeFactory extends AbstractFactory {
    public Shape getShape(String shapeType) {
        Shape shape = null;
        if ("circle".equals(shapeType)) {
            shape = new CircelShape();
        } else if ("rectangle".equals(shapeType)) {
            shape = new RectangleShape();
        }
        return shape;
    }

  public  public Color getColor(String colourType) {
        return null;
    }
}

public class ColorFactory extends AbstractFactory {
    public Color getColor(String colorType) {
        Color color = null;
        if ("red".equals(colorType)) {
            color = new RedColor();
        } else if ("green".equals(colorType)) {
            color = new GreenColor();
        }
        return color;
    }

    public Shape getShape(String shape) {
        return null;
    }

}

// This is the factory of factories
public class FactoryProducer {
    public static AbstractFactory getFactory(String factoryType) {
        AbstractFactory factory = null;
        if ("shape".equals(factoryType)) {
            factory = new ShapeFactory();
        } else if ("colour".equals(factoryType)) {
            factory = new ColorFactory();
        }
        return factory;
    }
}

public class AbstractFactoryPattern {
    public static void main(String[] args) {
        // Need a circel shape
        AbstractFactory shapeFactory = FactoryProducer.getFactory("shape");
        Shape circelShape = shapeFactory.getShape("circle");
        circelShape.draw();
        // Now colour
        AbstractFactory colourFactory = FactoryProducer.getFactory("colour");
        Color redColor = colourFactory.getColor("red");
        redColor.fill();

    }
}

No comments:

Post a Comment