Interpretation of the factory model of design patterns

The definition of the factory pattern: define a factory interface for creating objects, postpone the actual creation of objects to specific sub-factory classes.

                             This satisfies the feature of "separation of creation and use" required in the creation model.

Divided by actual business scenarios, the factory pattern has 3 different implementation methods, namely the simple factory pattern, the factory method pattern, and the abstract factory pattern.

Simple factory pattern: A specific factory class can generate multiple different product objects. The simple factory pattern does not belong to the 23 design patterns.

                         The method of creating an instance in the simple factory pattern is usually a static method, so the simple factory pattern is also called the static factory method pattern.

The realization of the simple factory pattern:

//产品接口
public interface Tea {
    void show();
}

//红茶
public class RedTea implements Tea{
     @Override
     public void show() {
         System.out.println("制作红茶!!!");
     }
}

//绿茶
public class GreenTea implements Tea{
    @Override
    public void show() {
        System.out.println("制作绿茶!!!");
    }
}

//工厂类
public class SimpleFactory {
    public static Tea makeTea(int type){
        if(type == 1){
            return new RedTea();
        }else if(type ==2){
            return new GreenTea();
        }else{
            System.out.println("制作失败!!!");
            return null;
        }
    }
}

//测试类
public class Test {
    public static void main(String[] args) {
        Tea tea = SimpleFactory.makeTea(1);
        tea.show();
    }
}

Simple factory model structure diagram:

The advantages of the simple factory pattern: 1. The factory class contains the necessary logical judgments to determine when to create an instance of which product.

                                    2. The client does not need to know the class name of the specific product created, only the parameters.

                                    3. It is also possible to introduce configuration files to replace and add new specific product categories without modifying the client code.

Disadvantages of the simple factory pattern: 1. The simple factory pattern has a single factory type, responsible for the creation of all products, and heavy responsibilities.

                                     2. The factory code will be very bloated and violate the principle of high aggregation.

                                    3. Using the simple factory model will increase the number of classes in the system and increase the complexity and difficulty of understanding the system.

                                    4. System expansion is difficult. Adding new products requires modifying the factory logic. When there are many types of products, the logic may be too complicated.

                                    5. The simple factory model uses the static factory method, which prevents the role of the factory from forming a hierarchical structure based on inheritance.

Factory method pattern: Define an interface for creating objects, and let the class that implements this interface decide which object to create.

                         The factory method is a further abstraction of the simple factory, which can introduce new products without modifying the original code and satisfy the open and close principle.

The realization of the factory method pattern:

//产品接口
public interface Tea {
    void make();
}

//红茶
public class RedTea implements Tea{
    @Override
    public void make() {
        System.out.println("制作红茶!!!");
    }
}

//绿茶
public class GreenTea implements Tea{
    @Override
    public void make() {
        System.out.println("制作绿茶!!!");
    }
}

//工厂接口
public interface Factory {
    Tea create();
}

//红茶工厂
public class RedTeaFactory implements Factory{
    @Override
    public Tea create() {
        return new RedTea();
    }
}

//绿茶工厂
public class GreenTeaFactory implements Factory{
    @Override
    public Tea create() {
        return new GreenTea();
    }
}

//测试类
public class FactoryMethodTest {
    public static void main(String[] args) {
        Factory redTeaFactory = new RedTeaFactory();
        Tea redTea = redTeaFactory.create();
        redTea.make();

        Factory greenTeaFactory = new GreenTeaFactory();
        Tea greenTea = greenTeaFactory.create();
        greenTea.make();
    }
}

Factory method pattern structure diagram:

The advantages of the factory method pattern: 1. The user only needs to know the name of the specific factory to get the desired product, and does not need to know the specific creation process of the product.

                                    2. Increased flexibility. For the creation of new products, you only need to write one more corresponding factory class.

                                    3. A typical decoupling framework. High-level modules only need to know the abstract class of the product, and don't need to care about other implementation classes.

Disadvantages of the factory method pattern: 1. The number of classes is easy to be too much, which increases the complexity.

                                    2. Increased the abstraction and difficulty of understanding the system.

                                    3. An abstract product can only produce one product.

Abstract factory pattern: provide an interface for creating a group of related or interdependent objects for the access class, and the access class can be obtained without specifying the specific class of the object

                         Products of the same product family. The abstract factory pattern is an upgraded version of the factory method pattern. The factory method pattern only produces one level of products.

                         The abstract factory model can produce multiple levels of products.

Product family: A group of products at different levels produced by the same specific factory is called a product family.

Product grade: The same type of product is called a product grade.

Implementation of the abstract factory pattern:

//茶接口
public interface Tea {
    void make();
}

//红茶
public class RedTea implements Tea{
    @Override
    public void make() {
        System.out.println("制作红茶!!!");
    }
}

//绿茶
public class GreenTea implements Tea{
    @Override
    public void make() {
        System.out.println("制作绿茶!!!");
    }
}

//奶接口
public interface Milk {
    void make();
}

//牛奶
public class NiuNai implements Milk{
    @Override
    public void make() {
        System.out.println("制作牛奶!!!");
    }
}

//羊奶
public class YangNai implements Milk{
    @Override
    public void make() {
        System.out.println("制作羊奶!!!");
    }
}

//工厂接口
public interface Factory {
    Tea create();

    Milk produce();
}

//工厂1
public class Factory1 implements Factory{
    @Override
    public Tea create() {
        return new RedTea();
    }

    @Override
    public Milk produce() {
        return new NiuNai();
    }
}

//工厂2
public class Factory2 implements Factory{
    @Override
    public Tea create() {
        return new GreenTea();
    }

    @Override
    public Milk produce() {
        return new YangNai();
    }
}

//测试类
public class AbstractFactoryTest {
    public static void main(String[] args) {
        Factory factory = new Factory1();
        Tea tea = factory.create();
        Milk milk = factory.produce();
        tea.make();
        milk.make();
    }
}

Structure diagram of abstract factory pattern:

The advantages of the abstract factory model: 1. The user only needs to know the name of the specific factory to get the desired product, without knowing the specific creation process of the product.

                                    2. A typical decoupling framework. High-level modules only need to know the abstract class of the product, and don't need to care about other implementation classes.

                                    3. The related multi-level products in the product family can be jointly managed within the class.

                                    4. When a product family is needed, the abstract factory can ensure that the client uses only one factory.

                                    5. The scalability of the program is enhanced. When a new product family is added, the original code does not need to be modified to meet the opening and closing principles.

Disadvantages of the abstract factory pattern: 1. When new products need to be added to the product family, all factory classes need to be modified.

                                    2. Increased the abstraction and difficulty of understanding the system.

Guess you like

Origin blog.csdn.net/wmqyp1415/article/details/114939681