2 Overview of factory method Modes
In simple factory mode, only one factory class is provided. The factory class is in the center of product class instantiation. It needs to know the creation details of each product object, and decide which product class to instantiate. The biggest drawback of the simple factory model is that when a new product is to be added to the system, the factory class must be modified and the necessary business logic must be added to it, which violates the "open and closed principle ". In addition, in the simple factory model, all products are created by the same factory, with heavy factory responsibilities and complicated business logic. The coupling between specific products and factories is high, the flexibility and scalability of the system are seriously affected, while the factory method mode can solve this problem well.
In the factory method mode, weInstead of providing a unified factory class to create all product objects, the system provides different factories for different products. The system provides a factory level structure corresponding to the product level structure.. The factory method mode is defined as follows:
Factory method pattern: defines an interface used to create an object, so that the subclass determines which class to instantiate. The factory method mode delays the instantiation of a class to its subclass. The factory method mode is also referred to as the factory pattern, or virtual constructor pattern or polymorphic factory pattern ). The factory method mode is a type creation mode. |
The factory method mode provides an abstract factory interface to declare the Abstract Factory method, and its subclass is used to implement the factory method and create a specific product object. The factory method mode structure 2 is shown below:
Figure 2 Structure of factory method mode
The factory method mode structure includes the following roles:
● Product (Abstract product ):It is the interface that defines the product. It is the super type of the object created in the factory method mode, that is, the public parent class of the product object.
● Concreteproduct (specific product ):It implements an abstract product interface. A specific type of product is created by a specific factory. The specific factory and specific product correspond one by one.
● Factory (Abstract Factory ):In the abstract factory class, the factory method is declared to return a product. Abstract Factory is the core of the factory method mode. All factory classes that create objects must implement this interface.
● Concretefactory (specific factory ):It is a subclass of the abstract factory class. It implements the factory methods defined in the abstract factory and can be called by the client to return an instance of a specific product class.
Compared with the simple factory mode, the most important difference between the factory method mode is that the abstract factory role is introduced. The abstract factory can be an interface, an abstract class, or a specific class. The typical code is as follows:
interface Factory { public Product factoryMethod();}
The factory method is declared in the abstract factory, but the factory method is not implemented. The creation of a specific product object is the responsibility of its subclass. The client can program the abstract factory and specify a specific factory class at runtime, the specific factory class implements the factory method. Different factories can create different specific products. The typical code is as follows:
class ConcreteFactory implements Factory { public Product factoryMethod() { return new ConcreteProduct(); }}
In actual use, in addition to creating a specific product object, the factory class can also initialize the product object and configure some resources and environments when implementing the factory method, for example, connecting to a database and creating a file.
In the client code, you only need to care about the factory class. Different factories can create different products. The typical client class code snippets are as follows:
...... Factory factory; factory = new concretefactory (); // you can use the configuration file to implement product; Product = factory. factorymethod ();......
You can use the configuration file to store the class name of a specific factory class concretefactory. You do not need to modify the source code when changing a new factory, making system expansion more convenient.
|
Thoughts Can the factory method in the factory method mode be a static method? Why? |
|
【Author】 Liu WeiHttp://blog.csdn.net/lovelion]