Design patterns: creational patterns and their corresponding codes: factory pattern, abstract factory pattern, singleton pattern, prototype pattern

Creational design pattern

Overview: Mainly used to encapsulate the instantiation logic of objects during the process of creating objects. It does this by defining a public interface for creating objects, but allowing subclasses to decide which class to instantiate.
Advantages: The object instantiation process can be decoupled from the client code, making the client code more flexible and scalable.

Example: This code segment is a public code segment

// 定义产品接口A
public interface ProductA {
    
    
    void operation();
}

// 实现具体产品A1
public class ConcreteProductA1 implements ProductA {
    
    
    @Override
    public void operation() {
    
    
        System.out.println("ConcreteProductA1 operation");
    }
}

// 实现具体产品A2
public class ConcreteProductA2 implements ProductA {
    
    
    @Override
    public void operation() {
    
    
        System.out.println("ConcreteProductA2 operation");
    }
}

// 定义产品接口B
public interface ProductB {
    
    
    void operation();
}

// 实现具体产品B1
public class ConcreteProductB1 implements ProductB {
    
    
    @Override
    public void operation() {
    
    
        System.out.println("ConcreteProductB1 operation");
    }
}

// 实现具体产品B2
public class ConcreteProductB2 implements ProductB {
    
    
    @Override
    public void operation() {
    
    
        System.out.println("ConcreteProductB2 operation");
    }
}

Factory pattern

Create a factory that only produces specific products

// 实现具体工厂1
public class ConcreteFactory1 implements AbstractFactory {
    
    
    @Override
    public ProductA createProductA() {
    
    
    	// 只生成产品A1
        return new ConcreteProductA1();
    }

    @Override
    public ProductB createProductB() {
    
    
    	// 只生成产品B1
        return new ConcreteProductB1();
    }
}

// 实现具体工厂2
public class ConcreteFactory2 implements AbstractFactory {
    
    
    @Override
    public ProductA createProductA() {
    
    
    	// 只生成产品A2
        return new ConcreteProductA2();
    }

    @Override
    public ProductB createProductB() {
    
    
    	// 只生成产品B2
        return new ConcreteProductB2();
    }
}

abstract factory pattern

Abstract factory, multiple factories can be created

// 抽象工厂接口
public interface AbstractFactory {
    
    
	// 负责创建A1、B1产品的工厂
    ProductA createProductA();
    // 负责创建A2、B2产品的工厂
    ProductB createProductB();
}

Finally run the factory code snippet

// 客户端代码
public class Client {
    
    
    public static void main(String[] args) {
    
    
    	// 创建多个工厂,以至于来生成特定工厂所支持的产品
        AbstractFactory factory1 = new ConcreteFactory1();
        AbstractFactory factory2 = new ConcreteFactory2();
        ProductA productA1 = factory1.createProductA();
        ProductB productB1 = factory1.createProductB();
        ProductA productA2 = factory2.createProductA();
        ProductB productB2 = factory2.createProductB();
        productA1.operation();
        productB1.operation();
        productA2.operation();
        productB2.operation();
    }
}

Singleton pattern

Ensure that a class has only one instance and provide a global access point to access the instance, but thread safety will occur (using locking method to solve it)

public class Singleton {
    
    
    // 私有的静态成员变量,用于保存单例的实例
    private static Singleton instance;

    // 私有的构造函数,防止外部代码通过new关键字创建该类的实例
    private Singleton() {
    
    
    }

    // 公共的静态方法,用于获取单例的实例
    public static synchronized Singleton getInstance() {
    
    
        if (instance == null) {
    
    
            instance = new Singleton();
        }
        return instance;
    }

    // 其他业务逻辑方法
    public void someMethod() {
    
    
        System.out.println("Singleton's someMethod");
    }
}

// 调用单例模式的实例
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        // 输出 true,说明两个变量指向同一个实例
        System.out.println(singleton1 == singleton2); 
        singleton1.someMethod();
        singleton2.someMethod();
    }
}

Prototype pattern

By copying an existing object to generate a new object without creating it through a constructor, the prototype pattern allows modification as needed

public class Prototype implements Cloneable {
    
    
    private String name;

    public Prototype(String name) {
    
    
        this.name = name;
    }

    public String getName() {
    
    
        return name;
    }
	
    @Override
    public Prototype clone() throws CloneNotSupportedException{
    
    
    	// 实现对象的浅拷贝
        return (Prototype) super.clone();
    }
}

// 使用原型模式
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Prototype prototype = new Prototype("Prototype");

        try {
    
    
            Prototype clone = prototype.clone();
            System.out.println(clone.getName()); // 输出 "Prototype"

            // 修改原型对象的属性值
            prototype.name = "Modified Prototype";
            System.out.println(prototype.getName()); // 输出 "Modified Prototype"
            System.out.println(clone.getName()); // 输出 "Prototype"
        } catch (CloneNotSupportedException e) {
    
    
            e.printStackTrace();
        }
    }
}

Guess you like

Origin blog.csdn.net/cleverstronge/article/details/131870806