[Design Pattern Notes] Abstract Factory Pattern

Create other factories around a gigafactory. The Gigafactory is also known as the factory of other factories.

Provides an optimal way to create objects.

In the abstract factory pattern, an interface is a factory responsible for creating a related object, without the need to explicitly specify their class.

Each generated factory can provide objects according to the factory pattern.

Intent: To provide an interface for creating a series of related or interdependent objects without specifying their concrete classes

Solve the problem: mainly solve the problem of interface selection

Key code: Aggregate multiple products of the same type in one factory


AbstractFactory {
    getA;
    getB;

}

AFactory extends AbsFactory{getA,getB};
BFactory extends AbsFactory{getA,getB};

AbstractFactory Afactory=FactoryProducer.getFactory("A");
A a=Afactory.getA;

AbstractFactory Bfactory=FactoryProducer.getFactory("B");
B b=Bfactory.getB;

 

Advantage: When multiple objects of a product family are designed to work together, it ensures that clients always only use objects from the same product family

Disadvantages: It is very difficult to expand the product family. To add a certain product in a series, it is necessary to add code in the abstract Creator and add code in the specific one.

Usage scenario: QQ skin change, a whole set change together

Case code:

Create a shape interface:

public interface Shape {
    void draw();
}

Shape implementation class:

public class Circle implements Shape{
    @Override
    public void draw() {
        System.out.println("circle draw");
    }
}
public class Rectangle implements Shape{
    @Override
    public void draw() {
        System.out.println("rectangle draw");
    }
}
public class Square implements Shape{
    @Override
    public void draw() {
        System.out.println("Square draw");
    }
}

Create an interface for colors:

public interface Color {
    void fill();
}
public class Red implements Color{
    @Override
    public void fill() {
        System.out.println("red fill");
    }
}
public class Blue implements Color{
    @Override
    public void fill() {
        System.out.println("Blue fill");
    }
}
public class Green implements Color{
    @Override
    public void fill() {
        System.out.println("Green fill");
    }
}

Create abstract classes for color and shape objects to get factories:

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

Inherit the abstract factory class and generate entity class objects based on the given information

public class ShapeFactory extends AbstractFactory{
    @Override
    public Color getColor(String color) {
        return null;
    }

    @Override
    public Shape getShape(String shape) {
        if(shape==null)return null;
        if(shape.equalsIgnoreCase("circle")){
            return new Circle();
        }else if(shape.equalsIgnoreCase("rectangle")){
            return new Rectangle();
        }else if(shape.equalsIgnoreCase("square")){
            return new Square();
        }
        return null;
    }
}
public class ColorFactory extends AbstractFactory{
    @Override
    public Color getColor(String color) {
        if(color==null)return null;
        else if(color.equalsIgnoreCase("red")){
            return new Red();
        }else if(color.equalsIgnoreCase("green")){
            return new Green();
        }else if(color.equalsIgnoreCase("blue")){
            return new Blue();
        }
        return null;
    }
    @Override
    public Shape getShape(String shape) {
        return null;
    }
}

Create a factory creator/generator class to get the factory by passing shape or color information

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

test:

public class AbstractFactoryPattrnDemo {
    public static void main(String[] args) {
        AbstractFactory shape = FactoryProducer.getFactory("shape");
        shape.getShape("circle").draw();
        shape.getShape("rectangle").draw();
        shape.getShape("square").draw();

        AbstractFactory color = FactoryProducer.getFactory("color");
        color.getColor("red").fill();
        color.getColor("green").fill();
        color.getColor("blue").fill();
    }
}

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124091955