Small talk on design patterns (16)—Abstract factory pattern

Column introduction

Column address

link

Column introduction

It mainly analyzes and summarizes the 23 common design patterns currently on the market one by one. I hope that interested friends can take a look and it will be continuously updated. I hope you can supervise me and we can learn and make progress together. Come on, everyone.
Insert image description here

abstract factory pattern

Abstract Factory Pattern is a creational design pattern that provides a way to encapsulate the creation of a set of related or interdependent objects without specifying their concrete classes.
Insert image description here

structure

AbstractFactory

Declare a set of methods for creating product objects, each method corresponding to the creation of a specific product class.

ConcreteFactory

Implement the abstract factory interface and be responsible for creating specific product objects.

AbstractProduct

Declare the common interface of products, and all specific product classes implement this interface.

ConcreteProduct

Implement abstract product interfaces and define the attributes and behaviors of specific products.
Insert image description here

Applicable situations

1

The system requires a set of related or interdependent product objects and wants to create them uniformly.

2

The system does not care about the creation process of specific products, only the interface of the product.

3

The system needs to provide a product class library without exposing the specific implementation.
Insert image description here

Java program implementation

First, we define the abstract product interface:

public interface Shape {
    
    
    void draw();
}

Then, we define the concrete product class to implement the abstract product interface:

public class Circle implements Shape {
    
    
    @Override
    public void draw() {
    
    
        System.out.println("Inside Circle::draw() method.");
    }
}

public class Rectangle implements Shape {
    
    
    @Override
    public void draw() {
    
    
        System.out.println("Inside Rectangle::draw() method.");
    }
}

Next, we define the abstract factory interface:

public interface ShapeFactory {
    
    
    Shape createShape();
}

Then, we define a concrete factory class that implements the abstract factory interface:

public class CircleFactory implements ShapeFactory {
    
    
    @Override
    public Shape createShape() {
    
    
        return new Circle();
    }
}

public class RectangleFactory implements ShapeFactory {
    
    
    @Override
    public Shape createShape() {
    
    
        return new Rectangle();
    }
}

Finally, we can use the abstract factory pattern to create concrete product objects:

public class Main {
    
    
    public static void main(String[] args) {
    
    
        ShapeFactory circleFactory = new CircleFactory();
        ShapeFactory rectangleFactory = new RectangleFactory();

        Shape circle = circleFactory.createShape();
        circle.draw();

        Shape rectangle = rectangleFactory.createShape();
        rectangle.draw();
    }
}

Output results

Inside Circle::draw() method.
Inside Rectangle::draw() method.

program analysis

We define the abstract product interface Shape and the concrete product classes Circle and Rectangle. Then, we defined the abstract factory interface ShapeFactory and the concrete factory classes CircleFactory and RectangleFactory. Finally, we created concrete product objects Circle and Rectangle using the abstract factory pattern.
Insert image description here

Advantages and Disadvantages Analysis

advantage

1

Provides a convenient way to create a set of related product objects, so that the client does not need to care about the creation details of specific products and only needs to use the products through abstract interfaces.

2

The client is decoupled from specific product categories, which enhances the flexibility and scalability of the system. Concrete factory classes and product classes can be easily replaced without affecting the client code.

3

In compliance with the opening and closing principle, when adding a new product family and product level structure, you only need to add the corresponding specific factory class and product class, without modifying the existing code.
Insert image description here

shortcoming

1

Increases the complexity and difficulty of understanding the system. Since the abstract factory pattern involves multiple abstract interfaces and concrete implementation classes, there are many classes and interfaces that need to be understood and managed, which increases the complexity of the code.

2

When a new product hierarchy needs to be added, the abstract factory interface and all concrete factory classes need to be modified, which violates the opening and closing principle.

3

When there are many types of products in a product family, the number of specific factory classes will increase, which increases the maintenance cost of the system.

Guess you like

Origin blog.csdn.net/weixin_74888502/article/details/133560513