设计模式之原型模式讲解

原型模式本身就是一种很简单的模式,在Java当中,由于内置了Cloneable 接口,就使得原型模式在Java中的实现变得非常简单。UML图如下:

我们来举一个生成新员工的例子来帮助大家理解。

复制代码
import java.util.Date;
public class Employee implements Cloneable {
    private String id;
    private String name;
    private Date hireDate;
    private transient Address address; // 注意:transient关键字表示此字段不参与序列化,这里假设地址不需要深拷贝
    public Employee(String id, String name, Date hireDate, Address address) {
        this.id = id;
        this.name = name;
        this.hireDate = (Date) hireDate.clone(); // 防止原始hireDate被修改
        this.address = address;
    }
    public String getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public Date getHireDate() {
        return (Date) hireDate.clone(); // 返回原始hireDate的副本
    }
    public Address getAddress() {
        return address;
    }
    // 重写Object的clone方法,实现浅拷贝
    @Override
    public Employee clone() throws CloneNotSupportedException {
        Employee clonedEmployee = (Employee) super.clone();
        // 如果有引用类型字段需要深拷贝,需要在这里进行额外处理
        // 例如,如果Address也需要深拷贝,可以添加如下代码:
        // clonedEmployee.address = address.clone();
        return clonedEmployee;
    }
}
class Address implements Cloneable {
    private String street;
    private String city;
    private String country;
    public Address(String street, String city, String country) {
        this.street = street;
        this.city = city;
        this.country = country;
    }
    // 提供Address的克隆方法实现深拷贝
    public Address clone() {
        return new Address(street, city, country);
    }
    // ... getters and setters ...
}
public class PrototypeDemo {
    public static void main(String[] args) {
        try {
            // 创建原始员工对象
            Address address = new Address("123 Main St", "Anytown", "USA");
            Employee original = new Employee("001", "John Doe", new Date(), address);
            // 使用clone方法创建新员工对象
            Employee cloned = original.clone();
            // 修改克隆对象的属性,验证克隆是否成功
            cloned.setName("Jane Doe");
            cloned.getAddress().setCity("Another City");
            System.out.println("Original Employee:");
            System.out.println("ID: " + original.getId());
            System.out.println("Name: " + original.getName());
            System.out.println("Hire Date: " + original.getHireDate());
            System.out.println("Address: " + original.getAddress());
            System.out.println("\nCloned Employee:");
            System.out.println("ID: " + cloned.getId());
            System.out.println("Name: " + cloned.getName());
            System.out.println("Hire Date: " + cloned.getHireDate());
            System.out.println("Address: " + cloned.getAddress());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
相关推荐
小bo波4 小时前
枚举实战
java·设计模式·枚举·后端开发·代码重构
不好听6137 小时前
Prompt 驱动 NLP:用大语言模型重新定义自然语言处理开发范式
设计模式·node.js·nlp
天文家9 小时前
深入理解装饰器与适配器:从设计模式到 Spring AOP 的工程实践
java·设计模式
workflower11 小时前
医院核心竞争力的四大重构
人工智能·安全·设计模式·重构·动态规划·scrum
折哥的程序人生 · 物流技术专研15 小时前
【电商多平台电子面单对接实战|第二篇】抖音代发电子面单对接:从“面条代码”到整洁架构的涅槃之路
设计模式·架构·系统架构·单元测试·代码规范·单一职责原则
葫芦和十三16 小时前
范式之变|Agent 设计,换语言了
人工智能·设计模式
ourenjiang16 小时前
【学习设计模式】原型模式
学习·设计模式·原型模式
贵慜_Derek16 小时前
《从零实现 Agent 系统》连载 20|MCP 与 Code Execution:协议、档位与 Sidecar
人工智能·设计模式·架构
Sam_Deep_Thinking2 天前
结算分摊的策略模式:不同营销活动的扣点计算方案
java·设计模式·架构·系统架构
故渊at2 天前
系列一:架构思想进阶 | 第3篇 SOLID 原则与设计模式实战:从“代码搬运工”到“架构师”的必经之路
观察者模式·设计模式·重构·架构·代理模式