One Design Pattern a Day - Factory Method

One: Pattern Motivation

Defines an interface for creating objects, but it is up to subclasses to decide which class to instantiate. Factory methods defer instantiation to subclasses.

Two: mode definition

The factory method pattern is also called the factory pattern, also called the virtual constructor (Virtual Constructor) pattern or the polymorphic factory (Polymorphic Factory) pattern, which belongs to the class creation pattern.
In the factory method pattern, the factory parent class is responsible for defining the public interface for creating product objects, while the factory subclass is responsible for generating specific product objects. The purpose of this is to delay the instantiation of the product class to the factory subclass. That is, through the factory subclass to determine which specific product class should be instantiated.

In a simple factory, the object is created by the parent class, while in a factory method, the object is created by the subclass.

Three: pattern structure

The Factory Method pattern includes the following roles:

  • AbstractProduct: abstract product
  • concreteProcuct: concrete product
  • AbstractFactory: abstract factory
  • ConcreteFactory: concrete factory
    insert image description here

Four: code example

There are four roles, abstract factory, concrete factory, abstract product, concrete product. It is no longer a factory class to instantiate a concrete product, but a subclass of an abstract factory to instantiate the product
1. Abstract product

public interface Bus {
    
    

    void run();

    void stop();
}

2. The concrete product ConcreteProduct
implements the interface

public class BusImpl1 implements Bus {
    
    
    @Override
    public void run() {
    
    
        System.out.println("BusImpl1 is running");
    }

    @Override
    public void stop() {
    
    
        System.out.println("BusImpl1 stop running");
    }
}

public class BusImpl2 implements Bus {
    
    
    @Override
    public void run() {
    
    
        System.out.println("BusImpl2 is running");
    }

    @Override
    public void stop() {
    
    
        System.out.println("BusImpl2 stop running");
    }
}

3. The abstract factory Factory
defines the product generation interface

/**
 * 抽象工厂
 */
public abstract class AbstractFactory {
    
    
    abstract public Bus create();
}

4. The concrete factory ConcreteFactory
implements the product generation interface and generates specific product role objects

/**
 * 具体工厂
 */
public class BusImpl1Factory extends AbstractFactory {
    
    
    @Override
    public Bus create() {
    
    
        return new BusImpl1();
    }
}

/**
 * 具体工厂
 */
public class BusImpl2Factory extends AbstractFactory {
    
    
    @Override
    public Bus create() {
    
    
        return new BusImpl2();
    }
}

5. Client:

public class Client1 {
    
    
    public static void main(String[] args) {
    
    
        // 由抽象工厂的子类去实例化产品
        AbstractFactory busImpl1Factory = new BusImpl1Factory();
        Bus busImpl1 = busImpl1Factory.create();
        busImpl1.run();

        AbstractFactory busImpl2Factory = new BusImpl2Factory();
        Bus busImpl2 = busImpl2Factory.create();
        busImpl2.stop();
    }
}

Five: Analysis and conclusion

  • The factory method pattern, also known as the factory pattern, belongs to the class creation pattern. In the factory method pattern, the factory parent class is responsible for defining the public interface for creating product objects, while the factory subclass is responsible for generating specific product objects. The purpose of this is to delay the instantiation of the product class to the factory subclass. That is, through the factory subclass to determine which specific product class should be instantiated.

  • A specific factory is responsible for producing specific products, and each specific factory corresponds to a specific product

  • The factory method pattern is a further abstraction and generalization of the simple factory pattern. Because of the use of object-oriented polymorphism, the factory method pattern maintains the advantages of the simple factory pattern and overcomes its disadvantages.

  • In the factory method pattern, the core factory class is no longer responsible for the creation of all products, but delegates the specific creation work to subclasses. This core class is only responsible for giving the interface that the concrete factory must implement, and not responsible for the details of the product class being instantiated, which makes the factory method pattern allow the system to introduce new products without modifying the role of the factory.

  • The main advantage of the factory method pattern is that there is no need to modify the existing system when adding a new product class, and it encapsulates the creation details of the product object, and the system has good flexibility and extensibility;

  • The disadvantage is that when new products are added, new factories need to be added, resulting in a paired increase in the number of system classes, which increases the complexity of the system to a certain extent.

  • The factory method pattern is applicable in the following situations:

  • A class for which a class does not know the objects it needs;

  • A class specifies which object to create through its subclasses;

  • The task of creating an object is delegated to one of the multiple factory subclasses, and the client can use it without caring which factory subclass creates the product subclass, and can specify it dynamically when needed.

Guess you like

Origin blog.csdn.net/qq_44867340/article/details/117635545