设计模式之原型模式讲解

原型模式本身就是一种很简单的模式,在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();
        }
    }
}
相关推荐
阿闽ooo7 小时前
深入浅出适配器模式:从跨国插头适配看接口兼容的艺术
c++·设计模式·适配器模式
Kiyra10 小时前
WebSocket vs HTTP:为什么 IM 系统选择长连接?
分布式·websocket·网络协议·http·设计模式·系统架构·wpf
山沐与山13 小时前
【设计模式】Python责任链模式:从入门到实战
python·设计模式·责任链模式
繁星星繁14 小时前
【项目】基于SDK实现的智能聊天助手(使用api接入deepseek)------(二)
c++·设计模式·学习方法
职业码农NO.114 小时前
系统架构设计中的 15 个关键取舍
设计模式·架构·系统架构·ddd·架构师·设计规范·领域驱动
燕双嘤14 小时前
LLM:RAG,设计模式,Agent框架
人工智能·机器学习·设计模式
阿拉斯攀登15 小时前
设计模式:构建者模式
设计模式·建造者模式·构建者模式
山沐与山15 小时前
【设计模式】Python工厂模式与依赖注入:FastAPI的Depends到底在干嘛
python·设计模式·fastapi
.简.简.单.单.16 小时前
Design Patterns In Modern C++ 中文版翻译 第十一章 享元模式
c++·设计模式·享元模式
BD_Marathon17 小时前
设计模式——类图
设计模式