The simple factory pattern does not fall into the 23 categoriesGoF(Gang of Four) one of the design patterns.

define

The simple factory pattern (also known as the Static method Factory pattern) is a creative design pattern in which a factory object determines which instance of a product class to create.

Usage scenarios

  • The factory class is responsible for creating fewer objects.
  • The customer only needs to know the parameters passed into the factory class, not the logic of creating the object.

role

The simple Factory pattern has the following roles:

  • Factory: Factory class, which is at the heart of the simple Factory pattern. It is responsible for implementing the internal logic that creates all instances. The method of creating a product class for a factory class can be called directly from the outside world to create the desired product object.

  • IProduct: Abstract product class, which is the parent of all objects created by the simple factory pattern and is responsible for describing the common interfaces of all instances.

  • **Product: ** Specific Product class, which is the creation target of the simple Factory pattern.

practice

Here we use the production of computers for example, suppose there is a computer contract manufacturer, it is already able to bring workers to produce Lenovo computers. As it expands, the foundry will also make HP and Asustek computers. We then need a single class dedicated to producing computers, which uses the simple factory pattern. Write down the specific code details:

  1. Start by creating an abstract product class for the computer, with an abstract method where the user starts the computer production:
Public abstract void start(); public abstract void start(); }Copy the code
  1. We then created each brand of computer that inherited its parent classComputerAnd implements the parent classstartMethods.
public class LenovoComputer extends Computer {

    @Override
    public void start() {
        System.out.println("Lenovo Computer Startup");
    }
}

public class HpComputer extends Computer {
    @Override
    public void start() {
        System.out.println("HP computer startup");
    }
}

public class AsusComputer extends Computer {
    @Override
    public void start() {
        System.out.println("Asus Computer startup"); }}Copy the code
  1. Next, create the factory class, which provides a static methodcreatComputerIt’s used to make computers. All you need to do is pass in the brand of computer you want to produce, and it will instantiate that brand of computer object.
public class ComputerFactory {
    public static Computer createComputer(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;
        }
        returnmComputer; }}Copy the code
  1. The client calls the factory class, passed inhpThat produces the HP computer and calls the computer objectstartMethods.
public class CreateComputer {
    public static void main(String[] args){
        ComputerFactory.createComputer("hp").start(); }}Copy the code

The advantages and disadvantages

Advantages: users can obtain the corresponding class instance according to the parameters, avoiding the direct instantiation of the class and reducing the degree of coupling.

Disadvantages: The instantiable type is already determined at compile time, and if you add a new type, you need to modify the factory, which violates the closure principle. A simple factory needs to know all the types to produce and is not suitable when there are too many subclasses or too many subclass levels.