The factory method belongs to the creation design pattern.
Design intent: Define an interface for creating an object, so that subclasses decide which class to instantiate, and the factory method defers the instantiation of a class to its subclasses.
The static factory uses object-oriented approach, effectively solves the problem of adding new products to the client and implementation class code modification, encapsulates the creation process, reduces the increase of the new product caused by the code modification errors, but the new features need to modify the client code and factory-created class of the judgment logic, Such a design violates the open-closed principle, open for expansion, and closed for modification, so we need to find a way to avoid adding new functionality when modifying the logic of the factory creation method. (After all, there will be changes to the original code will inevitably make mistakes)
The factory method is the design pattern that solves this problem effectively.
Class Diagram:
With the class diagram of the factory method pattern, you can see that the factory method pattern has four elements:
- Factory interface. The factory interface is the core of the factory method pattern, which interacts directly with the caller to provide product creation.
- Factory implementation. In programming, the factory implementation decides how to instantiate the product, is the way to realize the expansion, how many kinds of products need to have, how many specific factory implementation classes, each factory implementation class is responsible for creating a product.
- Product interface. The main purpose of the product interface is to define the product specification, and all product implementations must follow the specification of the product interface definition. Product interface is the most concern of the caller, the product interface definition directly determines the stability of the caller code.
- Product realization. To achieve the specific class of product interface, different products also need different product implementation classes, product implementation class and function to create the class corresponding.
Through the class diagram we can also see that each additional product adds a corresponding factory to create it, so that the entire plant and product system is not changed, but only the expansion of change, which is fully in line with the principle of open-closed.
Let me look at the code example:
Package com.factory.staticfactory.extend;/** * @author Gaoxu * Practice the truth! Product Interface */public interface IOperation {/** calculation method * @author Gaoxu * @return */public double Calculate ();}
The product interface defines how the product is handled.
Package com.factory.staticfactory.extend;/** * @author Gaoxu * Practice the truth! Abstract Parent class */public abstract class Abstractoperation implements ioperation{double NumA = 0;double NumB = 0;public double Getnuma () {return numA;} public void Setnuma (double numa) {This.numa = NumA;} Public double Getnumb () {return numB;} public void Setnumb (double numB) {this.numb = NumB;}}
Implements the interface and provides a common method of setting the data.
Package com.factory.staticfactory.extend;/** * @author Gaoxu * Practice the truth! */public class Operationadd extends abstractoperation{@Overridepublic double calculate () {return numa+numb;}}
Package com.factory.staticfactory.extend;/** * @author Gaoxu * Practice the truth! */public class Operationsub extends abstractoperation{@Overridepublic double calculate () {return numa-numb;}}
The addition, the hair reduction class implements the respective business logic.
Package Com.factory.factorymethod;import com.factory.staticfactory.extend.ioperation;/** Factory Method Interface * @author Gaoxu * Practice the truth! */public interface Ifactory {public ioperation createoperation ();}
Package Com.factory.factorymethod;import Com.factory.staticfactory.extend.ioperation;import com.factory.staticfactory.extend.operationadd;/** addition Factory class * @author Gaoxu * Practice the truth! */public class Addfactory implements Ifactory{public ioperation CreateOperation () {return new Operationadd ();}}
Package Com.factory.factorymethod;import Com.factory.staticfactory.extend.ioperation;import com.factory.staticfactory.extend.operationsub;/** Factory class * @author Gaoxu * Practice a genuine knowledge! */public class Subfactory implements Ifactory{public ioperation CreateOperation () {return new operationsub ();}}
Factory method is the further abstraction and promotion of static Factory, due to the use of polymorphism, the factory method mode to maintain the advantages of static factory and customer service its shortcomings, but the factory method of its own disadvantage is that each add a product needs to add a factory class, increased a lot of development workload.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Follow examples to learn design patterns-factory methods