Detailed explanation of image factory in Java design pattern

1、 Concept

Provides an interface to create a series of related or interdependent objects without specifying their specific classes.

Two, mode motivation

This series of objects are interdependent and equivalent to a product family

Three, mode structure

From the above figure, we can clearly see that the image factory mode includes the following four roles:

  1. AbstractFactory: the core of the image factory pattern, which has nothing to do with the specific business logic. It is usually a java interface or image class.

  2. Concrete factory: this role is usually closely related to specific business logic. The factory methods in this role instantiate specific products and return them according to specific business logic. The client obtains specific product objects through this role and calls the factory methods of this role. This role is usually assumed by a specific Java class.

  3. Image product role: the class in this role is the parent class of the product created by the factory method pattern, or the interface they jointly own, usually an interface or image class.

  4. Specific product role: any product created by the factory pattern is an instance of this role, which is assumed by a specific Java class.

The sample code is as follows:

According to the above pattern structure diagram, we briefly analyze "providing an interface to create a series of related or interdependent objects without specifying their specific classes":

1. Related or interdependent objects, in which the instance of producta1 and the instance of productb1 are a set of interrelated (such as internal correlation) or interdependent (such as whole and part) relationships. According to the business logic, producta1

It can only be associated with productb1 under the same product hierarchy abstractproductb, but not with productb2.

  2. It provides an interface for creating a series of related or interdependent objects without specifying their specific classes. The interfaces in this interface are abstractproducta and abstractproductb in the structure diagram. The client only depends on the interfaces of these products for programming without relying on specific implementation, that is, it complies with the dependency inversion principle. "There is no need to specify their specific classes", that is, the client does not know the existence of producta1, producta2, productb1 and productb2. The client only needs to call the factory method of the specific factory to return the specific product instance.

4、 Pattern example

We further analyze the example in the factory method mode. Now the factory producing tires is not satisfied with only producing car tires, He has now introduced the engine line, door line and other parts production lines of the whole car. It can be said that he can easily manufacture a car, but not all cars can be manufactured. For example, he can only produce Benz and BMW cars (such a factory is enough for NX). For example, now a car only contains tires, doors and engines (of course, there must be more than that), then the factory can produce BMW and Benz cars according to the customer's requirements, as shown in the figure below:

The code is as follows:

The operation results are as follows:

Mercedes Benz door open Benz start Benz color

According to the above class diagram and operation results, the following analysis can be made:

Benzdoor, benztire and benzengine are strongly related. We can say that a Benz car cannot use BMW door, that is, bmwdoor. This strong relationship is well maintained through benzpartfactory. For the client, such as the client class above, if the customer wants a Benz car, I only need a factory that produces Benz cars. All product instances of this factory are parts of Benz cars. We can also see from the running results.

Just imagine that with the development of this factory, it now also wants to produce Audi cars. At this time, we just need to add audidoor, auditire, audiengine and audipartfactory for Audi doors. Other classes do not need to be modified. But the customer said, I want to install a pair of wings on the car. I can fly in traffic jam. At this time, we need to add factory methods that can return wings to each factory and modify each factory, which is not in line with the opening and closing principle. Therefore, the abstract factory does not support the opening and closing principle for adding the product hierarchy, and supports the opening and closing principle for the product family dimension (such as Audi car).

5、 Pattern constraints

It is applicable to the product family that generates an interrelated or dependent product family and supports the extension in the product family direction, but not in the product level direction.

6、 Variations and extensions of patterns

1. The image factory provides static factory methods: the image factory can provide a static factory method and return specific factory instances through parameters.

2. Merging an image factory with a specific factory: if it is determined that there is only one product family in the product family direction, then the image factory is not necessary. At this time, only one specific factory is needed. We can further extend it to provide a static method for this specific factory, which returns its own instance.

7、 Relationship with other modes

If there is only one product hierarchy, it is the factory method mode, as shown in the following figure:

If there are multiple product hierarchies, each factory method in the image factory is the "factory method" mode.

8、 Advantages and disadvantages of the model

The opening and closing principle is supported in the direction of production port family, but not in the direction of production port hierarchical structure.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>