Factory Pattern: An Introductory Guide to Design Patterns that Simplify Object Creation and Improve Code Flexibility

preface:

Object creation and initialization is a common task in software development. In order to solve the problem of flexibility and scalability in the object creation process, the factory pattern came into being. This article will introduce the basic concepts and advantages of the factory pattern, as well as code examples suitable for beginners, to help readers understand and apply the factory pattern.

The concept of the factory pattern

The factory pattern is a design pattern for creating objects. It encapsulates the object instantiation process in a factory class to achieve the flexibility of object creation and initialization. The factory pattern decouples client code from the creation logic of concrete objects, providing a scalable and maintainable solution.

Advantages of the factory pattern

The main advantages of using the factory pattern include:

2.1. Encapsulation object creation logic:

Encapsulate the creation logic of the object in the factory class, so that the client code only focuses on the use of the object, and does not need to care about the specific creation details.

##2.2. Provide flexibility and scalability:
Through the factory class, we can easily add new product classes or modify the creation logic without affecting the client code.

2.3. To achieve loose coupling:

The client only needs to interact with the factory class, and does not need to directly depend on specific product classes, realizing a loosely coupled design.

Simple factory pattern example

The simple factory pattern is the most basic form of the factory pattern, which creates different types of objects through a factory class. The following is a code example of a simple factory pattern:

// 产品接口
interface Product {
    
    
    void operation();
}

// 具体产品类A
class ConcreteProductA implements Product {
    
    
    public void operation() {
    
    
        System.out.println("ConcreteProductA operation.");
    }
}

// 具体产品类B
class ConcreteProductB implements Product {
    
    
    public void operation() {
    
    
        System.out.println("ConcreteProductB operation.");
    }
}

// 工厂类
class SimpleFactory {
    
    
    public static Product createProduct(String type) {
    
    
        if (type.equals("A")) {
    
    
            return new ConcreteProductA();
        } else if (type.equals("B")) {
    
    
            return new ConcreteProductB();
        }
        return null;
    }
}

// 客户端代码
public class Main {
    
    
    public static void main(String[] args) {
    
    
        // 创建产品A
        Product productA = SimpleFactory.createProduct("A");
        productA.operation();

        // 创建产品B
        Product productB = SimpleFactory.createProduct("B");
        productB.operation();
    }
}

output:

ConcreteProductA operation.
ConcreteProductB operation.

Factory method pattern example

The factory method pattern is implemented by deferring the creation of objects to subclasses, and each specific product has a corresponding factory class. The following is a code example of the factory method pattern:

// 产品接口
interface Product {
    
    
    void operation();
}

// 具体产品类A
class ConcreteProductA implements Product {
    
    
    public void operation() {
    
    
        System.out.println("ConcreteProductA operation.");
    }
}

// 具体产品类B
class ConcreteProductB implements Product {
    
    
    public void operation() {
    
    
        System.out.println("ConcreteProductB operation.");
    }
}

// 工厂接口
interface Factory {
    
    
    Product createProduct();
}

// 具体工厂类A
class ConcreteFactoryA implements Factory {
    
    
    public Product createProduct() {
    
    
        return new ConcreteProductA();
    }
}

// 具体工厂类B
class ConcreteFactoryB implements Factory {
    
    
    public Product createProduct() {
    
    
        return new ConcreteProductB();
    }
}

// 客户端代码
public class Main {
    
    
    public static void main(String[] args) {
    
    
        // 创建工厂A
        Factory factoryA = new ConcreteFactoryA();
        // 通过工厂A创建产品A
        Product productA = factoryA.createProduct();
        productA.operation();

        // 创建工厂B
        Factory factoryB = new ConcreteFactoryB();
        // 通过工厂B创建产品B
        Product productB = factoryB.createProduct();
        productB.operation();
    }
}

output:

ConcreteProductA operation.
ConcreteProductB operation.

Abstract factory pattern example

The Abstract Factory pattern provides an interface for creating a series of related or interdependent objects, with each concrete factory class responsible for creating a specific line of products. The following is a code example of the abstract factory pattern:

java
Copy code
// 抽象产品接口A
interface AbstractProductA {
    
    
    void operationA();
}

// 具体产品类A1
class ConcreteProductA1 implements AbstractProductA {
    
    
    public void operationA() {
    
    
        System.out.println("ConcreteProductA1 operation.");
    }
}

// 具体产品类A2
class ConcreteProductA2 implements AbstractProductA {
    
    
    public void operationA() {
    
    
        System.out.println("ConcreteProductA2 operation.");
    }
}

// 抽象产品接口B
interface AbstractProductB {
    
    
    void operationB();
}

// 具体产品类B1
class ConcreteProductB1 implements AbstractProductB {
    
    
    public void operationB() {
    
    
        System.out.println("ConcreteProductB1 operation.");
    }
}

// 具体产品类B2
class ConcreteProductB2 implements AbstractProductB {
    
    
    public void operationB() {
    
    
        System.out.println("ConcreteProductB2 operation.");
    }
}

// 抽象工厂接口
interface AbstractFactory {
    
    
    AbstractProductA createProductA();
    AbstractProductB createProductB();
}

// 具体工厂类1
class ConcreteFactory1 implements AbstractFactory {
    
    
    public AbstractProductA createProductA() {
    
    
        return new ConcreteProductA1();
    }

    public AbstractProductB createProductB() {
    
    
        return new ConcreteProductB1();
    }
}

// 具体工厂类2
class ConcreteFactory2 implements AbstractFactory {
    
    
    public AbstractProductA createProductA() {
    
    
        return new ConcreteProductA2();
    }

    public AbstractProductB createProductB() {
    
    
        return new ConcreteProductB2();
}
}

// 客户端代码
public class Main {
    
    
public static void main(String[] args) {
    
    
// 创建工厂1
AbstractFactory factory1 = new ConcreteFactory1();
// 通过工厂1创建产品A1
AbstractProductA productA1 = factory1.createProductA();
// 通过工厂1创建产品B1
AbstractProductB productB1 = factory1.createProductB();


    productA1.operationA();
    productB1.operationB();

    // 创建工厂2
    AbstractFactory factory2 = new ConcreteFactory2();
    // 通过工厂2创建产品A2
    AbstractProductA productA2 = factory2.createProductA();
    // 通过工厂2创建产品B2
    AbstractProductB productB2 = factory2.createProductB();

    productA2.operationA();
    productB2.operationB();
}
}

output:

ConcreteProductA1 operation.
ConcreteProductB1 operation.
ConcreteProductA2 operation.
ConcreteProductB2 operation.

in conclusion:

Factory pattern is an important design pattern for creating objects. By encapsulating the creation logic of objects, the factory pattern enables code flexibility, scalability, and loose coupling. In this article, we introduced the Simple Factory pattern, Factory Method pattern, and Abstract Factory pattern with corresponding code examples. I hope that these examples can help readers understand the concept and application of the factory pattern, so that they can flexibly use the factory pattern to create objects in actual projects.

Guess you like

Origin blog.csdn.net/qq_46017342/article/details/131134550