JAVA design pattern 3: Abstract factory pattern, which is a creational design pattern

Author homepage : Designer Xiao Zheng
Author brief introduction : 3 years of JAVA full-stack development experience, focusing on JAVA technology, system customization, remote guidance, dedicated to enterprise digital transformation, certified lecturer of CSDN College and Blue Bridge Cloud Course.
Main directions : Vue, SpringBoot, WeChat applet

This article explains the Abstract Factory pattern in Java design patterns and gives sample code. The Abstract Factory pattern is a creational design pattern that provides an interface for creating a series of related or interdependent objects without specifying its concrete class.

Insert image description here


1. What is the abstract factory pattern?

The Abstract Factory pattern is a creational design pattern that provides an interface for creating a series of related or interdependent objects without specifying their concrete classes.

In the abstract factory pattern, there is an abstract factory interface which declares some methods for creating different products. The specific factory class implements this interface and is responsible for creating specific product objects. Each concrete factory class corresponds to a set of related products.

The core idea of ​​the abstract factory pattern is to separate the creation of objects from the use of objects . By using the abstract factory pattern, client code can create product objects through the factory interface without directly depending on the specific product class. In this way, the client can be decoupled from specific products, and the flexibility and scalability of the code can be improved.

The abstract factory pattern is applicable to the following 3 3There are 3 scenes, please study them carefully.

  1. A series of interrelated or interdependent product objects needs to be created.
  2. Which concrete product to create needs to be decided dynamically at runtime.
  3. It is necessary to hide the implementation details of specific products and only provide abstract interfaces to the outside world.

In summary, the abstract factory pattern decouples the creation and use of objects by providing an abstract factory interface and concrete factory implementation classes , and provides a flexible way to create a series of related product objects.

Insert image description here


2. Example of abstract factory pattern

Below is a simple Java code example that demonstrates how to use the abstract factory pattern to create buttons and text boxes for different operating systems.

First, we define an abstract GUI component interface and two specific implementation classes, the code is as follows.

// 抽象产品接口 - 按钮
interface Button {
    
    
    void render();
}

// 具体产品 - Windows按钮
class WindowsButton implements Button {
    
    
    @Override
    public void render() {
    
    
        System.out.println("Rendering a button in Windows style.");
    }
}

// 具体产品 - MacOS按钮
class MacOSButton implements Button {
    
    
    @Override
    public void render() {
    
    
        System.out.println("Rendering a button in MacOS style.");
    }
}

// 抽象产品接口 - 文本框
interface TextBox {
    
    
    void render();
}

// 具体产品 - Windows文本框
class WindowsTextBox implements TextBox {
    
    
    @Override
    public void render() {
    
    
        System.out.println("Rendering a text box in Windows style.");
    }
}

// 具体产品 - MacOS文本框
class MacOSTextBox implements TextBox {
    
    
    @Override
    public void render() {
    
    
        System.out.println("Rendering a text box in MacOS style.");
    }
}

Next, we define an abstract factory interface for creating GUI components, the code is as follows.

// 抽象工厂接口
interface GUIFactory {
    
    
    Button createButton();
    TextBox createTextBox();
}

// 具体工厂 - Windows工厂
class WindowsGUIFactory implements GUIFactory {
    
    
    @Override
    public Button createButton() {
    
    
        return new WindowsButton();
    }

    @Override
    public TextBox createTextBox() {
    
    
        return new WindowsTextBox();
    }
}

// 具体工厂 - MacOS工厂
class MacOSGUIFactory implements GUIFactory {
    
    
    @Override
    public Button createButton() {
    
    
        return new MacOSButton();
    }

    @Override
    public TextBox createTextBox() {
    
    
        return new MacOSTextBox();
    }
}

Finally, we use the abstract factory to create concrete GUI components, the code is as follows.

public class Main {
    
    
    public static void main(String[] args) {
    
    
        // 创建Windows风格的GUI组件
        GUIFactory windowsFactory = new WindowsGUIFactory();
        Button windowsButton = windowsFactory.createButton();
        TextBox windowsTextBox = windowsFactory.createTextBox();

        windowsButton.render();
        windowsTextBox.render();

        // 创建MacOS风格的GUI组件
        GUIFactory macOSFactory = new MacOSGUIFactory();
        Button macOSButton = macOSFactory.createButton();
        TextBox macOSTextBox = macOSFactory.createTextBox();

        macOSButton.render();
        macOSTextBox.render();
    }
}

The output results will vary according to different operating system styles, for example, under Windowsthe system, the output will be as follows.

Rendering a button in Windows style.
Rendering a text box in Windows style.

While MacOSunder the system, the output will be as shown below.

Rendering a button in MacOS style.
Rendering a text box in MacOS style.

By using the abstract factory pattern, we can create different styles of GUI components based on different factories without directly relying on specific product classes.

This can improve the flexibility of the code, allowing us to easily switch and extend different styles of GUI components.

Insert image description here


3. Application scenarios of abstract factory pattern

The abstract factory pattern is suitable for the following scenarios:

  1. Need to create a series of related or interdependent product objects : The abstract factory pattern can help us create a series of related product objects that have certain associations or dependencies between them. For example, in a game, we may need to create characters, weapons, and equipment from different camps, and there is a certain correlation between these products.
  2. Need to dynamically decide which specific product to create at runtime : The abstract factory pattern allows us to decide which specific product to create based on conditions or configuration at runtime. This flexibility allows us to create different products based on different needs.
  3. It is necessary to hide the implementation details of specific products : the abstract factory pattern decouples the creation and use of products. The client only needs to pay attention to the abstract factory interface and product interface, and does not need to care about the implementation details of specific products. This improves code maintainability and scalability.
  4. The principle of opening and closing needs to be satisfied : the abstract factory pattern can satisfy the principle of opening and closing, that is, it is open for extension and closed for modification. We can easily extend the functionality of the Abstract Factory pattern by adding new concrete factories and product classes without modifying existing code.

The abstract factory pattern is suitable for situations where you need to create a series of related or interdependent product objects, and decide to create specific products based on conditions or configuration at runtime . It can help us achieve code flexibility, maintainability and scalability.

Insert image description here


4. Abstract Factory Pattern Interview Questions

  1. What is the abstract factory pattern? What problem does it solve?
  2. What is the difference between abstract factory pattern and factory method pattern?
  3. What are the roles of the abstract factory pattern?
  4. Can you give an example of a simple abstract factory pattern?
  5. In what scenarios do you think the abstract factory pattern is best used?

5. Summary

This article explains the abstract factory pattern in the Java design pattern, and gives sample code. In the next blog, I will explain the Java prototype pattern.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_41464123/article/details/132635973