Comic: What is the "Abstract Factory Model"?

Author | Xiao Hui

Source | Programmer Xiaohui (ID: chengxuyuanxiaohui)

The so-called "factory pattern" is a collective term for three common design patterns, which are simple factory pattern, factory method pattern, and abstract factory pattern.

In the last issue of the comic, we introduced the characteristics and application scenarios of the simple factory model and the factory method model. Those who haven’t read it can read this article: Comic: "Factory Model" of Design Patterns

In this issue, we will introduce the abstract factory pattern and the application of the factory pattern in the Spring framework.

For example, the business needs to create three products: masks, gas masks, and protective clothing, and each product has two categories: high-end and low-end. According to the solution of the factory method model, the categories that need to be created are as follows:

As shown in the figure, each product category corresponds to a factory category. When the number of products is large, the number of factory categories will be older and larger, making the system very complicated.

What should we do at this time?

First look at the code of the product class . Masks and protective clothing are two abstract interfaces, which have high-end and low-end implementation classes respectively:

public interface IMask {
    void showMask();
}

public class LowEndMask implements IMask {
    @Override
    public void showMask(){
        System.out.println("我的低端口罩");
    }
}

public class HighEndMask implements IMask {
    @Override
    public void showMask() {
        System.out.println("我是高端口罩");
    }
}

public interface IProtectiveSuit {
    void showSuit();
}

public class LowEndProtectiveSuit implements IProtectiveSuit {
    @Override
    public void showSuit() {
        System.out.println("我是低端防护服");
    }
}

public class HighEndProtectiveSuit implements IProtectiveSuit {
    @Override
    public void showSuit() {
        System.out.println("我是高端防护服");
    }
}

Next is the factory category . Since the products are divided into high-end and low-end groups, the factories are also divided into high-end factories and low-end factories, each responsible for the creation of products in the group:

public interface IFactory {
    //创建口罩
    IMask createMask();
    //创建防护服
    IProtectiveSuit createSuit();
}

public class LowEndFactory implements IFactory {
    @Override
    public IMask createMask() {
        IMask mask =  new LowEndMask();
        // .....
        //  LowEndMask的100行初始化代码
        return mask;
    }

    @Override
    public IProtectiveSuit createSuit() {
        IProtectiveSuit suit =  new LowEndProtectiveSuit();
        // .....
        //  LowEndProtectiveSuit的100行初始化代码
        return suit;
    }
}

public class HighEndFactory implements IFactory {
    @Override
    public IMask createMask() {
        IMask mask =  new HighEndMask();
        // .....
        // HighEndMask的100行初始化代码
        return mask;
    }

    @Override
    public IProtectiveSuit createSuit() {
        IProtectiveSuit suit =  new HighEndProtectiveSuit();
        // .....
        //  HighEndProtectiveSuit的100行初始化代码
        return suit;
    }
}

Finally, the client code can create different products by instantiating different factory subclasses and calling different creation methods:

public class Test {

    public static void main(String[] args) {
        IFactory factoryA = new LowEndFactory();
        IFactory factoryB = new HighEndFactory();
        //创建低端口罩
        IMask maskA = factoryA.createMask();
        //创建高端口罩
        IMask maskB = factoryB.createMask();
        //创建低端防护服
        IProtectiveSuit suitA = factoryA.createSuit();
        //创建高端防护服
        IProtectiveSuit suitB = factoryB.createSuit();

        maskA.showMask();
        maskB.showMask();
        suitA.showSuit();
        suitB.showSuit();
    }
}

Simple factory pattern:

The simple factory model has a unique factory class. The creation method of the factory class makes an if-else condition judgment based on the parameters passed in to determine what product object will be created.

Factory method pattern:

The factory method pattern implements the factory interface by multiple factory classes, and uses polymorphism to create different product objects, thereby avoiding lengthy if-else condition judgments.

Abstract factory pattern:

The abstract factory pattern groups product subcategories, and different products in the same group are created by different methods of the same factory subcategory, thereby reducing the number of factory subcategories.


Friends who are familiar with the spring framework must know an important feature of spring: dependency injection (DI).

With Spring's dependency injection, developers do not need to manually instantiate bean objects in business code, nor do they need to know any factory classes.

The entire process from creation to destruction of the bean object is completely managed by the spring container. All the user needs to do is to set various attributes of the bean in the xml configuration file (or use annotations):

<bean id="userController" class="com.xiaohui.controller.UserController">
    <constructor-arg name="userService" ref="userService"></constructor-arg>
</bean>

<bean id="userService" class="com.xiaohui.service.UserService">

According to the above configuration, the spring container will dynamically create the UserController object and create the UserService object that the UserController depends on.

If the developer wants to change the implementation class of the bean object userService to another class, there is no need to change any code, just modify the class attribute of the corresponding bean in the configuration file.

In most cases, we use the new keyword to create an object, and the class to which the object belongs is clearly defined in the code.

But in a few cases, we need to use the meta-information of the class (such as the complete class name) to dynamically create objects during the running of the program, which uses Java reflection.

When we configure the corresponding bean in the spring configuration file and start the project, spring will parse the xml configuration file for us, and create bean objects from the "factory" inside spring according to the different life cycles of the bean.

Friends who are interested in the principle of spring dependency injection can read the BeanFactory interface in the spring source code and the related implementation classes.


更多精彩推荐
☞没有 5G 版 iPhone 的苹果秋季发布会,发布了些什么?
☞对话阿里云李飞飞:云原生数据库的时代来了
☞IT 往事录:苹果 Mac 之父,却在 Mac 问世前黯然退场
☞B 站神曲damedane:精髓在于换脸,五分钟就能学会
☞可怕!公司部署了一个东西,悄悄盯着你……
☞极简椭圆曲线密码学入门
点分享点点赞点在看

Guess you like

Origin blog.csdn.net/csdnnews/article/details/108633605