6.4. Abstract factory pattern

发布时间 : 2025-10-25 13:34:16 UTC      

Page Views: 9 views

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.

6.4.1. Introduction

意图: 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.

6.4.2. Realize

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

抽象工厂模式的 UML 图

6.4.3. Step 1

Create an interface for the shape.

Shape.java

publicinterfaceShape{voiddraw();} 

6.4.4. Step 2

Create an entity class that implements the interface.

Rectangle.java

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

Create an interface for the color.

Color.java

publicinterfaceColor{voidfill();} 

6.4.6. Step 4

Create an entity class that implements the interface.

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

Create abstract classes for the Color and Shape objects to get the factory.

AbstractFactory.java

publicabstractclassAbstractFactory{publicabstractColorgetColor(Stringcolor);publicabstractShapegetShape(Stringshape);} 

6.4.8. Step 6

Create a factory class that extends AbstractFactory and generate objects of the entity class based on the given information.

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

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

FactoryProducer.java

publicclassFactoryProducer{publicstaticAbstractFactorygetFactory(Stringchoice){if(choice.equalsIgnoreCase("SHAPE")){returnnewShapeFactory();}elseif(choice.equalsIgnoreCase("COLOR")){returnnewColorFactory();}returnnull;}} 

6.4.10. Step 8

Use FactoryProducer to get AbstractFactory and pass type information to get objects of entity classes.

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

Execute the program and output the result:

Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method. Inside Red::fill() method. Inside Green::fill() method. Inside Blue::fill() method. 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。