The Abstract Factory pattern (Abstract Factory Pattern) is about creating other factories around one super factory. The super factory is also known as the factory of other factories. This type of design pattern is a creative pattern, which provides the best way to create objects.
In the abstract factory pattern, interfaces are factories responsible for creating related objects, and there is no need to explicitly specify their classes. Each generated factory can provide objects according to the factory pattern. 意图: Provides an interface for creating a series of related or interdependent objects without specifying their specific classes. 主要解决: The main problem is to solve the problem of interface selection. 何时使用: The products of the system have more than one product family, and the system consumes only one of the products. 如何解决: Define multiple products within a product family. 关键代码: Aggregate multiple similar products in one factory. 应用实例: Work, in order to attend some parties, there must be two or more sets of clothes, such as business clothes (sets, a series of specific products), fashion clothes (sets, a series of specific products), and even for a family, there may be business women’s wear, business men’s wear, fashion women’s wear, fashion men’s wear, these are also complete sets, that is, a series of specific products. Suppose there is a situation (it does not exist in reality, otherwise, there is no way to enter communism, but it is conducive to illustrating the abstract factory model), in your home, a certain wardrobe (specific factory) can only store one such clothes (complete sets, a series of specific products), and every time you take this complete set of clothes, you naturally have to take them out of this wardrobe. To understand with the idea of OOP, all wardrobes (concrete factories) are of wardrobe category (abstract factories), and each complete set of clothes includes specific tops (a specific product), trousers (a specific product), these concrete tops are actually tops (abstract products), and specific trousers are trousers (another abstract product). 优点: When multiple objects in a product family are designed to work together, it ensures that the client always uses only objects in the same product family. 缺点: Product family expansion is very difficult, to add a series of products, not only to add code in the abstract Creator, but also to add code in the concrete. 使用场景: 1. QQ change the skin, change the whole set together. 2. Generate programs of different operating systems. 注意事项: The product family is difficult to expand and the product grade is easy to expand. We will create Shape And Color Interfaces and entity classes that implement these interfaces. The next step is to create an abstract factory class AbstractFactory . Then define the factory class ShapeFactory And ColorFactory Both factory classes are extended AbstractFactory . Then create a factory creator / generator class FactoryProducer . AbstractFactoryPatternDemo Class usage FactoryProducer To get AbstractFactory Object. It will be directed to AbstractFactory Pass shape information Shape ( CIRCLE / RECTANGLE / SQUARE To get the type of object it needs At the same time, it also points to AbstractFactory Transmit color information Color ( RED / GREEN / BLUE To get the type of object it needs Create an interface for the shape. Create an entity class that implements the interface. Rectangle.java Create an interface for the color. Create an entity class that implements the interface. Create abstract classes for the Color and Shape objects to get the factory. Create a factory class that extends AbstractFactory and generate objects of the entity class based on the given information. Create a factory creator / generator class to get the factory by passing shape or color information. Use FactoryProducer to get AbstractFactory and pass type information to get objects of entity classes. Execute the program and output the result: 6.4.1. Introduction ¶
6.4.2. Realize ¶
6.4.3. Step 1 ¶
Shape.java ¶
publicinterfaceShape{voiddraw();}
6.4.4. Step 2 ¶
Rectangle.java ¶
publicclassRectangleimplementsShape{@Overridepublicvoiddraw(){System.out.println("Inside
Rectangle::draw() method.");}}
Square.java ¶
publicclassSquareimplementsShape{@Overridepublicvoiddraw(){System.out.println("Inside
Square::draw() method.");}}
Circle.java ¶
publicclassCircleimplementsShape{@Overridepublicvoiddraw(){System.out.println("Inside
Circle::draw() method.");}}
6.4.5. Step 3 ¶
Color.java ¶
publicinterfaceColor{voidfill();}
6.4.6. Step 4 ¶
Red.java ¶
publicclassRedimplementsColor{@Overridepublicvoidfill(){System.out.println("Inside
Red::fill() method.");}}
Green.java ¶
publicclassGreenimplementsColor{@Overridepublicvoidfill(){System.out.println("Inside
Green::fill() method.");}}
Blue.java ¶
publicclassBlueimplementsColor{@Overridepublicvoidfill(){System.out.println("Inside
Blue::fill() method.");}}
6.4.7. Step 5 ¶
AbstractFactory.java ¶
publicabstractclassAbstractFactory{publicabstractColorgetColor(Stringcolor);publicabstractShapegetShape(Stringshape);}
6.4.8. Step 6 ¶
ShapeFactory.java ¶
publicclassShapeFactoryextendsAbstractFactory{@OverridepublicShapegetShape(StringshapeType){if(shapeType==null){returnnull;}if(shapeType.equalsIgnoreCase("CIRCLE")){returnnewCircle();}elseif(shapeType.equalsIgnoreCase("RECTANGLE")){returnnewRectangle();}elseif(shapeType.equalsIgnoreCase("SQUARE")){returnnewSquare();}returnnull;}@OverridepublicColorgetColor(Stringcolor){returnnull;}}
ColorFactory.java ¶
publicclassColorFactoryextendsAbstractFactory{@OverridepublicShapegetShape(StringshapeType){returnnull;}@OverridepublicColorgetColor(Stringcolor){if(color==null){returnnull;}if(color.equalsIgnoreCase("RED")){returnnewRed();}elseif(color.equalsIgnoreCase("GREEN")){returnnewGreen();}elseif(color.equalsIgnoreCase("BLUE")){returnnewBlue();}returnnull;}}
6.4.9. Step 7 ¶
FactoryProducer.java ¶
publicclassFactoryProducer{publicstaticAbstractFactorygetFactory(Stringchoice){if(choice.equalsIgnoreCase("SHAPE")){returnnewShapeFactory();}elseif(choice.equalsIgnoreCase("COLOR")){returnnewColorFactory();}returnnull;}}
6.4.10. Step 8 ¶
AbstractFactoryPatternDemo.java ¶
publicclassAbstractFactoryPatternDemo{publicstaticvoidmain(String[]args){//获取形状工厂AbstractFactoryshapeFactory=FactoryProducer.getFactory("SHAPE");//获取形状为
Circle 的对象Shapeshape1=shapeFactory.getShape("CIRCLE");//调用 Circle
的 draw 方法shape1.draw();//获取形状为 Rectangle
的对象Shapeshape2=shapeFactory.getShape("RECTANGLE");//调用 Rectangle 的
draw 方法shape2.draw();//获取形状为 Square
的对象Shapeshape3=shapeFactory.getShape("SQUARE");//调用 Square 的 draw
方法shape3.draw();//获取颜色工厂AbstractFactorycolorFactory=FactoryProducer.getFactory("COLOR");//获取颜色为
Red 的对象Colorcolor1=colorFactory.getColor("RED");//调用 Red 的 fill
方法color1.fill();//获取颜色为 Green
的对象Colorcolor2=colorFactory.getColor("Green");//调用 Green 的 fill
方法color2.fill();//获取颜色为 Blue
的对象Colorcolor3=colorFactory.getColor("BLUE");//调用 Blue 的 fill
方法color3.fill();}}
6.4.11. Step 9 ¶
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside Red::fill() method.
Inside Green::fill() method.
Inside Blue::fill() method.