Creation mode-factory method mode

Creation mode-factory method mode

(I. Overview

The factory method pattern defines an interface for creating objects, allowing subclasses to decide which class to instantiate. Factory Method is that the instantiation of a class is delayed to its subclasses.

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 the subclasses. This core class has been transformed into an abstract factory role, which is only responsible for giving the interfaces that the concrete factory subclass must implement, without touching the details of which product class should be instantiated.

Insert picture description here

(2) Mode composition

There are mainly the following roles in the factory method pattern:

  1. Abstract factory (Creator) role: This role is the core of the factory method pattern, which has nothing to do with the application. Any factory class that creates objects in the pattern must implement this interface. In the above system, this role is played by the Java interface Creator; in the actual system, this role is often implemented using abstract Java classes.
  2. Concrete Factory (Concrete Creator) role: This role is the concrete Java class that implements the abstract factory interface. The specific factory role contains logic closely related to the application and is called by the application to create product objects.
  3. Abstract product (Product) role: the supertype of the object created by the factory method pattern, that is, the common parent or common interface of the product object. In this system, this role is played by the Java interface Product; in actual systems, this role is often implemented using abstract Java classes.
  4. Concrete Product (Concrete Product) role: This role implements the interface declared by the abstract product role. Each object created by the factory method pattern is an instance of a specific product role.

Insert picture description here

(3) Use steps

The factory method pattern is very simple to use, mainly divided into the following five steps:

Step 1: Create an abstract factory class and define the public interface of the concrete factory;
Step 2: Create an abstract product class and define the public interface of the concrete product;
Step 3: Create a concrete product class (inherit the abstract product class) and define the specific product produced;
Step 4: Create a concrete factory class (inherit the abstract factory class) and define the method for creating corresponding concrete product instances;
Step 5: The outside world creates instances of different concrete product classes by calling the methods of the concrete factory class

Let's look at a specific example to experience the factory method pattern:

Step 1: Create an abstract factory class and define the public interface of the concrete factory

abstract class Factory{
    
    
    public abstract Product Manufacture();
}

Step 2: Create an abstract product class and define the public interface of the specific product

abstract class Product{
    
    
    public abstract void Show();
}

Step 3: Create a concrete product class (inherit the abstract product class) and define the concrete product to be produced

class ProductA extends Product{
    
    
    @Override
    public void Show() {
    
    
        System.out.println("生产产品A");
    }
}
class ProductB extends Product{
    
    
    @Override
    public void Show() {
    
    
        System.out.println("生产产品B");
    }
}

Step 4: Create a concrete factory class (inherit the abstract factory class) and define the method to create corresponding concrete product instances

class FactoryA extends Factory{
    
    
    @Override
    public Product Manufacture() {
    
    
        return new ProductA();
    }
}
class FactoryB extends Factory{
    
    
    @Override
    public Product Manufacture() {
    
    
        return new ProductB();
    }
}

Step 5: The outside world creates instances of different specific product classes by calling methods of specific factory classes

//生产工作流程
public class FactoryPattern {
    
    
    public static void main(String[] args){
    
    
        FactoryA mFactoryA = new FactoryA();
        mFactoryA.Manufacture().Show();

        FactoryB mFactoryB = new FactoryB();
        mFactoryB.Manufacture().Show();
    }
}

(4) Features

As the twin brother of the abstract factory pattern, the factory method pattern defines an interface for creating objects, but the subclass determines which class to instantiate, which means that the factory method pattern delays the instantiation to the subclass.

The factory method model is very consistent with the "opening and closing principle". When a new product needs to be added, we only need to add a specific product category and a specific factory corresponding to it, without modifying the original system. At the same time, in the factory method model, the user only needs to know the specific factory where the product is produced. It does not need to be related to the creation process of the product, and even the specific product category name does not need to be known. However, although he is well in line with the "opening and closing principle", because each new product needs to add two categories, this will inevitably lead to an increase in the complexity of the system, which is also one of his biggest flaws.

July 10, 2020

Guess you like

Origin blog.csdn.net/weixin_43907422/article/details/107242109