Factory model OverviewThe factory mode belongs to the class creation mode, and the factory mode is responsible for instantiating a large number of classes with common interfaces. The factory mode dynamically determines which class to instantiate without having to know which class to instantiate each time in advance. The factory mode has three basic forms: 1. Simple factory mode, also known as static factory methodpattern ). 2. Factory method mode, also known as polymorphic factory mode or virtual constructor mode. 3. Abstract Factory mode, also known as toolkit or toolkit mode.
Simple factory ModeWe use legends to understand the basic semantics of the simple factory model. (ALL of the following diagrams are only representative forms of understanding. To express clearly, some aspects may violate the basic semantics in modeling illustrations ).
Figure 1
The master of the simple factory model plays an important role in learning the singleton mode and the multi-instance mode. Figure 1 shows the fruit planting on the farm. Obviously, we know that the fruit interface class has three methods (plant, harvest, grow). Because the apple and grape classes implement the fruit interface, therefore, all methods in the fruit class must be implemented. Apple and grape have their own related methods. As for the relationship between them, I will not list the code. The farm gardener (fruitgardener) is also part of the system, naturally represented by a suitable class. This class is the fruitgardener class. The fruitgardener class creates different fruit objects, such as Apple and grape instances, based on client requirements. If an invalid requirement is received, the fruitgardener class throws a badfruitexception.
| ************ * *******/Public class fruitgardener {/***** static factory Method */public static Fruit Factory (string which) throws badfruitexception {If (which. inclusignorecase ("apple") {return new Apple ();} else if (which. repeated signorecase ("grape") {return New grape ();} else {Throw new badfruitexception ("bad fruit request ");}}} ************ * ********/Public CIA SS badfruitexception extends exception {public badfruitexception (string MSG) {super (MSG );}} **************** *****/.................. Try {fruitgardener. factory ("grape"); fruitgardener. factory ("apple"); fruitgardener. factory ("XXX");} catch (badfruitexception e ){...} ..................... |
It can be seen that a simple factory involves three roles: 1. A factory role is responsible for creating product objects. The above fruitgardener class. It is the core of the factory method model. It is often implemented by a specific class. 2. Abstract Product role: implements the product's Common product, which can be implemented by abstract classes or interfaces. Fruit3. specific product: a specific product, such as Apple and grape.
Note:1. The factory role in the factory method can also be assumed by the abstract Product role. The abstract Product role creates an object. For Java APIs such as Java. Text. dateformat, the abstract Product role is defined as an abstract class rather than an interface. (The differences and applications between abstract classes and interfaces will be listed later ). An abstract class cannot have its own instance. The dateformat factory method is a static method, not a common method. That is to say, it is directly related to the class itself and does not pass the class instantiation.
Figure 22. If the abstract Product role is omitted, the factory role can be merged with the specific product role.
| Public class fruitgardener {public fruitgardener () {}/ *** static factory Method */public static fruitgardener Factory () {return New fruitgardener ();}} |
The preceding two modes are similar to the singleton mode and multi-class mode, but not the singleton mode or multi-instance mode. It will be mentioned later in the pattern design. Don't worry. This is a degradation of the simple factory model. The following code:
Relationship between the simple factory model and other models1. Relationship with Singleton mode the singleton mode uses the simple factory mode. In other words, the singleton class has a static factory method to provide its own instance. An abstract product class is also a subclass factory. (2) but the singleton mode is not a degradation of the simple factory mode. The Singleton mode requires that the construction method of the singleton class is private, so that the client cannot directly instantiate it, the static factory method must be used to instantiate the instance, and the singleton class itself is its own factory role. In other words, the singleton class is responsible for creating its own instance. The Singleton class uses a static property to store its own unique instance. The factory method always provides only this instance. (Speaking of this, I am a little excited. I finally know why these models are used in the Java API provided by Sun.) 2. The relationship with the Multi-sample mode multi-sample mode is the promotion of the single-sample mode. The common feature of the Multi-sample mode and Singleton mode is that they prohibit external entities from directly instantiating them, and provide external instances that are used cyclically through static factory methods. They differ in that the singleton mode has only one instance, while the multiple-case mode can have multiple instances. The multi-instance mode usually has a clustering attribute. By registering an existing instance with this clustering attribute, you can use the instance cyclically. Generally, a typical multi-instance class has a certain internal state, which can be used to differentiate various instances. For each internal state, only one instance exists. Note: Clustering indicates that the relationship between classes is the relationship between the whole and the part, which is divided into: Shared aggregation and composition; the part of shared aggregation can be a part of multiple groups; components only indicate the relationship between the whole and the part. If the whole disappears, the part disappears. It should be noted that some object-oriented masters have different definitions of aggregation. It is not difficult for others to understand the multi-sample mode. We can understand it through Java API, especially the clustering relationship. Based on the parameters passed in by the outside world, the factory method can query its own registration aggregation. If an instance in this status already exists, it is directly provided to the outside world; otherwise, create a new instance that meets the requirements, register it in the cluster, and then provide it to the client. Details about the singleton mode and multi-case mode are described later. 3. the memorandum mode singleton and multi-sample modes use an attribute or clustering attribute to register the created product object, in this way, you can query this property or aggregate property to find and share the created product objects. This is the application of the memorandum model. 4. the MVC pattern is not a strict design pattern, but an architectural pattern at a higher level. The MVC pattern can be divided into several combinations of design patterns, including the merging pattern, strategy pattern, Observer pattern, and may also include the decoration pattern, Mediator Pattern, iteration subpattern, and factory pattern. Objects Created in the simple factory mode often belong to a product level structure, which can be a view in the MVC mode, while the factory role itself can be a controller ). One MVC mode can have one controller and multiple views, as shown in.
Figure 3 we use the simple factory method to understand that the controller (Controller) is also the factory role. (fruitgardener) is responsible for creating the product view ). If the system requires multiple controllers to participate in this process, the simple factory mode is not applicable. We should consider using the factory method mode and describe it later.
Application of simple factory methodsThe static factory method is used to hide the instantiation of a specific subclass, so that the client does not have to consider how to instantiate a specific subclass, because the abstract class dateformat will provide an appropriate instance of a specific subclass. This is an excellent application of a simple factory method model. The real type of a product category is hidden by its superclass, which provides system scalability. If a new subclass is added to the system in the future, the factory class can replace the objects handed over to the client with the instances of the new subclass without affecting the client. This practice of setting the return type of factory methods to the abstract product type is called for abstract programming, which is an application of the dependency inversion principle (DIP. We will describe the specific application of the "Dependency inversion principle (DIP)" later.
Disadvantages of this modeIt is not hard to see that if a new product, such as orange, needs to modify the factory class (fruitgardener), thus limiting its flexibility. It is suitable for product angle classes, therefore, simple factories only support the "open-close" principle to a limited extent. The factory class integrates all the creation work, and it seems that the factory class is relatively negative. In case of a problem, all creation work cannot be performed. When the product class has different interface types, the factory class must determine when to create a product. This combination of timing and specific product judgment logic makes it more difficult for the system to expand functions in the future. This disadvantage is overcome in the factory method model. Because the simple factory mode uses the static method as the factory method, but the static method cannot be inherited by subclass, the factory role cannot form a hierarchy based on inheritance. This disadvantage will be overcome in the factory method model.
Well, the simple factory model in the factory model is described here. The next section mainly describes the factory methods in the factory model.