Design Pattern 2 Factory Method Pattern

What is the factory method pattern

We have a product class, a factory interface, and a factory class that implements the factory interface. The creation of product objects relies on the factory class to create. The core is the separation of creation and use.

Define an interface for creating an object,but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses

The factory method pattern has the following advantages:

  • Products can be obtained through the factory without knowing the specific creation process of the product

  • If you add a new product, you do not need to modify the factory, just add a new factory

However, this has also become a disadvantage of the factory model, which undoubtedly increases the complexity of the system.

If you want to implement an abstract factory, you need several elements:

  • Abstract factory

  • Specific factory

  • Abstract product

  • Specific products

The structure diagram is as follows:

AbstractFactoryIt is an abstract factory, which is actually an interface with a newProduct()method to be implemented to create a product.

ConcreteFactoryFor the concrete factory, for AbstractFactorythe realization. There may be multiple factories.

ProductIt is the interface of the product.

ConcreteProductIt is the realization of specific products, and there is a one-to-one correspondence between general products and factories.

Code

Having said that, let's write a Demo to realize it.

First write a product interface:

public interface Product {
    void show();
}

Let's write a factory interface:

public interface Factory {
    public Product product();
}

Specific product realization:

public class ConcreteProduct1 implements Product {
    @Override
    public void show() {
        System.out.println("具体产品1显示...");
    }
}

Specific factory realization:

public class ConcreteFactory1 implements Factory {
    @Override
    public Product product() {
        System.out.println("具体工厂1生成-->具体产品1...");
        return new ConcreteProduct1();
    }
}

This factory is used to create products.

If we want to get the product object, we only need to instantiate the factory

@Slf4j
public class FactoryTest {
    @Test
    public void test() {
        ConcreteFactory1 concreteFactory1 = new ConcreteFactory1();
        Product product = concreteFactory1.product();
        product.show();
    }
}

Test Results:

具体工厂1生成-->具体产品1...
具体产品1显示...

Some thoughts on the factory method pattern

The product itself is not instantiated, but is handed over to the factory to do it. The factory method is that the instantiation of a class is delayed to the factory class.

I think an important advantage of using the factory method is to block the product category. The caller can obtain the product without knowing the details of the generated product, which ensures the privacy and security of the produced product.

For example, if you use JDBC to connect to the database, if you migrate the database from MySQL to Oracle. Only need to change the database driver. Nothing else needs to be changed. The factory method pattern is used here.

Of course, if you add product categories, you need to carefully consider whether to add factory categories. After all, this will increase system complexity.

The factory method pattern can be used in heterogeneous projects, such as interacting with a non-Java project through WebService. Although WebService claims to be able to achieve the isomorphism of heterogeneous systems, there are still many problems in actual development. , Such as type issues, WSDL file support issues, etc. The objects generated from WSDL are considered to be a product, and then managed by a specific factory class to reduce coupling with peripheral systems.

In complex applications, a multi-factory approach is generally used, and then a coordination class is added to avoid communication between the caller and each sub-factory. The role of the coordination class is to encapsulate the sub-factory class and provide a unified access interface for high-level modules.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/108799052