2018年8月22日19:34:48
Factory method Pattern Definition
I want to marry Qing, will ask Qing's parents, that is the life of parents.
Factory method Mode (Factory), which defines an interface for creating objects, so that subclasses decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses. --"design pattern: The basis of reusable object-oriented software"
Usage Scenarios
The factory method mode, based on the simple factory model, submits the choice of judgment logic to the client, allowing the client to decide which specific factory class to use. This is a disadvantage of factory methods compared to the simple factory model, so you'll need to know beforehand what the specific factory class is. Relative to the straightforward new object, do not need to remember the specific product class, the service side hides the specific product class creation details, in the specific factory class is not necessarily to create the specific product class, but also can do some other things, in line with the object-oriented encapsulation.
Simple Factory mode Plus products, the need to modify the internal judgment logic, which violates the design pattern of the six modes of opening and closing principle (digging pit), is open to the extension, the modification is closed. Factory method mode Plus products, only need to increase the corresponding specific plant objects, in line with the opening and shutting principle, which is one of the advantages of factory method model. Personal point of view: The factory method model is suitable for a scene that is constantly changing (that is, adding and deleting) specific product objects, taking advantage of this advantage. such as clothing seasons, you are a designer, seasonal new products shelves, plus new products, the boss choose a new factory, sales business only need to obtain new products from the factory, the old products under the shelf, and the old factory contract expires, so.
Role
After the factory role of the simple factory model is passed on to the client, it encapsulates the creation of object details to the specific factory class, and the specific factory classes inherit from a common interface. Simple Factory mode to view: Simple Factory mode.
The factory method pattern has the following four types of roles:
- Abstract Factory Role (Ifactory): Specific factory public interface, define factory method return product
- Specific factory Role (Concretefactoryimpl): Inherit factory factory public interface, implement factory method, return to specific product instance during operation
- Abstract product roles (iproduct)
- Specific product roles (CONCRETEPRODUCTIMPL)
Icon
code example
The code example still uses the subtraction operation, just like the simple Factory mode, simply splitting the factory class Simplefactory into abstract factory classes and specific factory classes. Simple Factory mode to view: Simple Factory mode.
Class Dependency diagram:
Abstract Factory Role (Ioperationfactory.java):
public interface IOperationFactory { public IOperation createOperation();}
Abstract out the specific factory object public interface.
Specific factory roles (Addoperationfactoryimpl.java, Suboperationfactoryimpl.java, Muloperationfactoryimpl.java, Divoperationfactoryimpl.java):
// 加法工厂public class AddOperationFactoryImpl implements IOperationFactory { @Override public IOperation createOperation() { return new AddOperationImpl(); }}// 减法工厂public class SubOperationFactoryImpl implements IOperationFactory { @Override public IOperation createOperation() { return new SubOperationImpl(); }}// 乘法工厂public class MulOperationFactoryImpl implements IOperationFactory { @Override public IOperation createOperation() { return new MulOperationImpl(); }}// 除法工厂public class DivOperationFactoryImpl implements IOperationFactory { @Override public IOperation createOperation() { return new DivOperationImpl(); }}
Each specific factory object corresponds to a specific product object.
Abstract product Role (Iproduct.java):
public interface IOperation { public int getResult(int a, int b);}
Specific product roles (Addoperationimpl.java, Suboperationimpl.java, Muloperationimpl.java, Divoperationimpl.java):
// 加法实现类public class AddOperationImpl implements IOperation{ @Override public int getResult(int a, int b) { return a + b; }}// 减法实现类public class SubOperationImpl implements IOperation { @Override public int getResult(int a, int b) { return a - b; }}// 乘法实现类public class MulOperationImpl implements IOperation { @Override public int getResult(int a, int b) { return a * b; }}// 除法实现类public class DivOperationImpl implements IOperation { @Override public int getResult(int a, int b) { return a / b; }}
Test Class (Factorymethodtest.java):
public class Factorymethodtest {public static void main (string[] args) {int a = 999, b = 888; Add//Create specific factory ioperationfactory operationfactory = new Addoperationfactoryimpl (); Create a specific product ioperation operation = Operationfactory.createoperation (); Invoke the function of the specific product int result = Operation.getresult (A, b); 1887 System.out.println (Result); Minus operationfactory = new Suboperationfactoryimpl (); Operation = Operationfactory.createoperation (); result = Operation.getresult (A, b); 111 System.out.println (Result); Multiply operationfactory = new Muloperationfactoryimpl (); Operation = Operationfactory.createoperation (); result = Operation.getresult (A, b); 887112 System.out.println (Result); Except operationfactory = new Divoperationfactoryimpl (); Operation = Operationfactory.createoperation (); result = OperatioN.getresult (A, b); 1 System.out.println (result); } }
The client wants to determine what specific factory object to use, thereby instantiating the specific product object that is required.
Advantages (Small copy)
(link at the end of the text reference)
1, in the factory method model, the factory method is used to create the customer needs of the product, but also to the customer hides which specific product class will be instantiated this detail, the user only need to care about the required products corresponding to the factory, no care about the creation of details, or even need not know the specific product class name.
2. Polymorphism design based on plant role and product role is the key to the factory method model. It enables the factory to determine which product objects are created autonomously, and the details of how to create the object are completely encapsulated inside a specific factory. The factory method pattern is also called the Polymorphic factory pattern because all the specific factory classes have the same abstract parent class.
3, another advantage of using the factory method mode is to add a new product to the system without modifying the interface provided by the abstract factory and the abstract product, without modifying the client, or modifying other specific factories and specific products, and adding a specific factory and specific products. In this way, the scalability of the system becomes very good, fully conform to the "open and close principle".
Disadvantages (Small copy)
1, in the addition of new products, the need to write a new product class, but also to provide the corresponding specific factory class, the number of classes in the system will increase in pairs, to a certain extent, increase the complexity of the system, there are more classes need to compile and run, will bring some additional cost to the system.
2, due to the scalability of the system, the need to introduce an abstraction layer, in the client code are used in the abstraction layer to define, increase the system's abstraction and understanding of the difficulty, and in the implementation may need to use the DOM, reflection and other technologies, increase the system implementation difficulty.
Summarize
The factory method model is a further abstraction and generalization of the simple factory model.
Due to the use of object-oriented polymorphism, the factory method pattern preserves the advantages of a simple factory model and overcomes its drawbacks.
All code visible in Github.mingmingcome
Finish
Reference:
Factory Methods Mode (Factory method pattern)