Factory method pattern


I. Schema definition

  • The Factory Method Pattern is also called the Factory Pattern, the Virtual Constructor Pattern or the Polymorphic Factory Pattern, which belongs to the class creation model
  • In the Factory method pattern, the factory parent class is responsible for defining the common interface for creating product objects, while the factory subclass is responsible for generating concrete product objects. The purpose of this is to defer the instantiation of the product class to the factory subclass to determine which concrete product class should be instantiated
  • The factory parent class only gives the interfaces that a particular factory must implement, not the details of which products are instantiated, allowing the factory method pattern to allow the system to introduce new products without changing the factory role

Second, pattern structure

The factory method pattern has four roles: abstract products are interfaces that define products, and they are supertypes of objects created by the factory method pattern, i.e. common parent classes or interfaces of product objects; Concrete products implement abstract product interfaces, and concrete products of a certain type are created by specific concrete factories, often corresponding to each other. The abstract factory declares a factory method that returns a product. It is the core of the factory method pattern, and any factory class that creates objects in the pattern must implement this interface. A concrete factory is a subclass of an abstract factory class that implements factory methods defined in an abstract factory and can be called by a customer to return an instance of a concrete product class

Third, calculator problem

interface Factory{    // Factory interface
    OPeration creatOperation(a);
}
​
/*** * factory to implement this interface ***/
class AddFactory implements Factory{
    public Operation creatOperation(a){
        return newAdd(); }}class SubFactory implements Factory{
    public Operation creatOperation(a){
        return newSub(); }}class MulFactory implements Factory{
    public Operation creatOperation(a){
        return newMul(); }}class Divfactory implements Factory{
    public Operation creatOperation(a){
        return newDiv(); }}abstract class Operation{
    double a, b;
    abstract double getResult(a) throws Exception;
}
​
class Add extends Operation{
    double getResult(a){
        returna + b; }}class Sub extends Operation{
    double getResult(a){
        returna - b; }}class Mul extends Operation{
    double getResult(a){
        returna * b; }}class Div extends Operation{
    if(b == 0) {throw new Exception("The divisor cannot be zero.");
    }
    return a / b;
}
​
public static void main(String[] args) throws Exception{
    Factory factory = new AddFactory();       // Create a factory
    Operation op = factory.creatOperation();  // Create a product
    op.a = 1;
    op.b = 2;
    double res = op.getResult();
}
Copy the code
The class diagram

Four, advantages and disadvantages

advantages
  • In the factory method pattern, the factory method is used to create the product that the customer needs, and it also hides the details of which specific product class will be instantiated from the customer. The user only needs to care about the factory for the desired product, not the creation details, or even the class name of the specific product class
  • The polymorphism design based on factory role and product role is the key of factory method pattern. It enables the factory to decide which product object to create, and the details of how to create this object are completely encapsulated within the specific factory. The factory method pattern is also called polymorphic factory pattern because all concrete factory classes have the same abstract parent class
  • Another advantage of using the factory method pattern is that when you add a new product to the system, you don’t need to modify the abstract factory and the interface provided by the abstract product, you don’t need to modify the client, or you don’t need to modify other concrete factories and concrete products, you just need to add a concrete factory and concrete products. In this way, the scalability of the system becomes very good, fully in accordance with the “open and closed principle”
disadvantages
  • When adding new products, it is necessary to write new concrete product classes and provide corresponding concrete factory classes. The number of classes in the system will increase in pairs, which increases the complexity of the system to a certain extent. More classes need to be compiled and run, which will bring some extra overhead to the system
  • Considering the scalability of the system, it is necessary to introduce an abstraction layer, which is defined in the client code, increasing the abstractness and difficulty of understanding of the system, and DOM, reflection and other technologies may be used in the implementation, increasing the difficulty of the implementation of the system