Small talk on design patterns (9)-Factory method 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

Factory method pattern

The factory method pattern is a creational design pattern that defines an interface for creating objects, but it is up to the subclass to decide which class to instantiate. The factory method pattern defers object instantiation to subclasses.

Role classification

Abstract Product

It defines the interface of the product and is the common parent class or interface of the specific product class.

Concrete Product

A concrete class that implements the abstract product interface.

Abstract Factory

Defines the interface for creating products, including one or more abstract methods for creating products.

Concrete Factory

It implements the abstract factory interface and is responsible for instantiating specific products.
Insert image description here

main idea

Separating the creation and use of objects, the client creates objects by calling factory methods instead of directly instantiating specific products. The advantage of this is that the client only needs to know the existence of abstract products and abstract factories without caring about the details of specific products. When you need to create different types of products, you only need to implement the corresponding specific products and specific factories without modifying the client code.

Insert image description here

Java code implementation:

Suppose there is a car factory that can produce different types of cars, including cars and SUVs. First define an abstract car class (AbstractProduct):

public abstract class Car {
    
    
    public abstract void drive();
}

Then define the specific car class (ConcreteProduct1) and SUV class (ConcreteProduct2), which both inherit from the abstract car class:

public class SedanCar extends Car {
    
    
    @Override
    public void drive() {
    
    
        System.out.println("Driving sedan car...");
    }
}

public class SUV extends Car {
    
    
    @Override
    public void drive() {
    
    
        System.out.println("Driving SUV...");
    }
}

Next define the abstract car factory class (AbstractFactory), which contains an abstract method for creating cars:

public abstract class CarFactory {
    
    
    public abstract Car createCar();
}

Then define the specific car factory class (ConcreteFactory1) and SUV factory class (ConcreteFactory2), which both inherit from the abstract car factory class:

public class SedanCarFactory extends CarFactory {
    
    
    @Override
    public Car createCar() {
    
    
        return new SedanCar();
    }
}

public class SUVFactory extends CarFactory {
    
    
    @Override
    public Car createCar() {
    
    
        return new SUV();
    }
}

Finally, use the factory method in client code to create the car object:

public class Client {
    
    
    public static void main(String[] args) {
    
    
        CarFactory factory1 = new SedanCarFactory();
        Car sedanCar = factory1.createCar();
        sedanCar.drive();
        
        CarFactory factory2 = new SUVFactory();
        Car suv = factory2.createCar();
        suv.drive();
    }
}

The output result is

Driving sedan car...
Driving SUV...

analyze

Through the factory method pattern, the client code only needs to interact with the abstract product and abstract factory without caring about the creation process of the specific product. When you need to add other types of cars, you only need to implement the corresponding specific products and specific factories without modifying the client code, achieving code scalability and maintainability.
Insert image description here

Advantages and Disadvantages Analysis

advantage

Comply with the opening and closing principle

The factory method pattern makes the system more scalable by introducing the concepts of abstract factory and concrete factory. When you need to add a new product, you only need to add the corresponding specific product and specific factory, without modifying the existing code, which is consistent with the opening and closing principle.

Encapsulates the object creation process

The client only needs to care about abstract products and abstract factories, and does not need to care about the creation process of specific products. The creation process of specific products is encapsulated in specific factories, making the client code more concise and readable.

Reduces the coupling between clients and specific products

The client only relies on abstract products and abstract factories, not specific products. This can decouple the client code from the specific product and improve the flexibility and maintainability of the code.

Specific factory classes can be dynamically specified through configuration files, etc.

The factory method pattern can dynamically specify specific factory classes through configuration files, reflection, etc., thereby achieving a more flexible object creation method.
Insert image description here

shortcoming

Increases the complexity of the system

The introduction of the concepts of abstract factory and concrete factory makes the structure of the system more complex. If there are only a few products in the system, using the factory method pattern may appear to be too complex to maintain and understand.

Increased the amount of code

The factory method pattern requires the definition of multiple classes such as abstract products, concrete products, abstract factories, and concrete factories, which increases the amount of code. For simple projects, using the factory method pattern may appear redundant.

The client needs to know the specific factory class

The client needs to know the existence of the specific factory class, which increases the client's dependency. If the creation logic of a specific factory class changes, the client code also needs to be modified accordingly.

Insert image description here

Guess you like

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