The simple factory pattern of design patterns (SimpleFactoryPattern)

1. Definition

      • The simple factory pattern belongs to the creational pattern, also called the static factory method pattern, which is an instance of which product class is created by a factory object.

      • In real life, the factory is responsible for the production of products; also in the design pattern, the simple factory pattern can be understood as a class responsible for the production of objects, called the "factory class".

2. Mode principle

      2.1 Mode composition

 

Composition (role)

relationship

effect

Abstract product category (Product)

The parent category of the specific product

Describe the public interface of the product

Concrete Product

Subclass of abstract product; target class created by factory class

Describe specific products

Factory

The core class of the simple factory pattern

Called directly by the outside world to create the required product objects according to the conditions

 

 

 

 

 

 

 

 2.2 Pattern UML diagram

                                                        UML graphics

 2.3 Use steps

           •    Create an abstract product class and define the public interface of a specific product;

           • Create concrete product classes (inherited from abstract product classes) and define the concrete products to be produced;

           • Create a factory class , which provides a static method that can create instances of different specific products according to different conditions.

           • The client calls the factory class and completes the creation of different specific product instances by passing in different parameters.

3. Simple realization of simple factory pattern

     3.1 Example description:

            Suppose there is a computer foundry manufacturer, and it can already manufacture Lenovo computers. As the business expands, the foundry manufacturer will also produce computers from HP and Asus. In this way, we need a separate class to specialize in the production of computers, which uses the simple factory pattern.

     3.2 Implementation steps

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

public abstract class Computer {
    public abstract void start();
}

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

public class LenovoComputer extends Computer{
    @Override
    public void start() {
        System.out.println("联想计算机启动");
    }
}

public class HpComputer extends Computer{
    @Override
    public void start() {
        System.out.println("惠普计算机启动");
    }
}

public class AsusComputer extends Computer{
    @Override
    public void start() {
        System.out.println("华硕计算机启动");
    }
}

Step 3 : Create a factory class for external calls, and create instances of different specific product classes based on the different parameters passed in through static methods

public class ComputerFactory {
    public static Computer createrComputer(String type) {
        Computer mComputer = null;
        switch (type) {
            case "lenovo":
                mComputer = new LenovoComputer();
                break;
            case "hp":
                mComputer = new HpComputer();
                break;
            case "asus":
                mComputer = new AsusComputer();
                break;
        }
        return mComputer;
    }
}

Step 4 : The client calls the factory class

public class CreateComputer {
    public static void main(String[] args) {
        ComputerFactory.createrComputer("hp").start();
    }
}

4. The advantages and disadvantages of the simple factory model

4.1 Advantages

  • Separate the work of creating an instance from the work of using an instance, and the user does not need to care about how the class object is created to achieve decoupling;
  • Put the work of initializing the instance in the factory to make the code easier to maintain. More in line with object-oriented principles & interface-oriented programming, rather than implementation-oriented programming.

4.2 Disadvantages

  • The factory class concentrates the creation logic of all instances (products). Once the factory fails to work normally, the entire system will be affected;
  • Violating the "open-close principle", once a new product is added, the factory logic has to be modified, which will cause the factory logic to be too complicated.
  • Because the simple factory model uses static factory methods, static methods cannot be inherited or rewritten, which will cause the factory role to fail to form a hierarchy based on inheritance.

5. Use scenarios of the simple factory model

  • If the customer only knows the parameters passed into the factory class, and does not care about the logic of how to create the object;
  • When the factory class is responsible for creating fewer objects (specific products).

 

Guess you like

Origin blog.csdn.net/zhourui_1021/article/details/83628110