写点什么

设计模式的艺术 第七章原型设计模式练习(在某销售管理系统中设计并实现了一个客户类 Customer,其中包含一个名为客户地址的成员变量,客户地址的类型为 Address。用浅克隆和深克隆分别实现 Customer 对象的复制)

作者:代廉洁
  • 2022 年 9 月 06 日
    浙江
  • 本文字数:2892 字

    阅读完需:约 9 分钟

Sunny 软件公司在某销售管理系统中设计并实现了一个客户类 Customer,其中包含一个名为客户地址的成员变量,客户地址的类型为 Address。用浅克隆和深克隆分别实现 Customer 对象的复制,并比较这两种克隆方式的异同。


一、类结构图

浅克隆:


深克隆:


二、典型实现代码

浅克隆:

具体顾客类:具体原型类
// 具体顾客类:具体原型类public class Customer implements Cloneable {    private String name;    private Address address;
public Customer(String name) { this.name = name; }
public void setName(String name) { this.name = name; }
public String getName() { return this.name; }
public void setAddress(Address address) { this.address = address; }
public Address getAddress() { return address; }
@Override public Customer clone() { Object object = null; try { object = super.clone(); } catch (CloneNotSupportedException e) { System.err.println("Not support cloneable"); } return (Customer) object; }}
复制代码


具体地址类:具体克隆原型成员类
// 具体地址类:具体克隆原型成员类public class Address {    private String province;    private String city;    private String street;
public String getProvince() { return province; }
public void setProvince(String province) { this.province = province; }
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
public String getStreet() { return street; }
public void setStreet(String street) { this.street = street; }}
复制代码


客户端代码:
public class Client {    public static void main(String[] args) {        Customer customer;        Address address;
customer = new Customer("张无忌"); address = new Address(); address.setProvince("省份一"); address.setCity("城市一"); address.setStreet("街道一"); customer.setAddress(address); System.out.println(customer.getName() + "的地址:" + customer.getAddress().getProvince() + customer.getAddress().getCity() + customer.getAddress().getStreet());
Customer customer2; customer2 = customer.clone(); System.out.println(customer2.getName() + "的地址:" + customer2.getAddress().getProvince() + customer2.getAddress().getCity() + customer2.getAddress().getStreet()); }}
复制代码


编译并运行程序,输出如下结果:
张无忌的地址:省份一城市一街道一张无忌的地址:省份一城市一街道一
复制代码


深克隆:

抽象顾客原型类:抽象原型类
// 抽象顾客原型类:抽象原型类public abstract class CustomerPrototype implements Serializable {    public abstract CustomerPrototype deepClone();}
复制代码


具体顾客类:具体原型类
// 具体顾客类:具体原型类public class ConcreteCustomer extends CustomerPrototype {    private String name;    private CustomerAddress customerAddress;
public ConcreteCustomer(String name) { this.name = name; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public CustomerAddress getCustomerAddress() { return customerAddress; }
public void setCustomerAddress(CustomerAddress customerAddress) { this.customerAddress = customerAddress; }
@Override public CustomerPrototype deepClone() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(this);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); try { return (CustomerPrototype) ois.readObject(); } catch (ClassNotFoundException e) { return null; } } catch (IOException e) { return null; } }}
复制代码


具体地址类:具体克隆原型成员类
// 具体地址类:具体克隆原型成员类public class CustomerAddress implements Serializable {    private String province;    private String city;    private String street;
public String getProvince() { return province; }
public void setProvince(String province) { this.province = province; }
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
public String getStreet() { return street; }
public void setStreet(String street) { this.street = street; }}
复制代码


客户端代码:
public class Client {    public static void main(String[] args) {        ConcreteCustomer concreteCustomer;        CustomerAddress customerAddress;
customerAddress = new CustomerAddress(); customerAddress.setProvince("省份二"); customerAddress.setCity("城市二"); customerAddress.setStreet("街道二"); concreteCustomer = new ConcreteCustomer("张无忌"); concreteCustomer.setCustomerAddress(customerAddress); System.out.println(concreteCustomer.getName() + "的地址:" + concreteCustomer.getCustomerAddress().getProvince() + concreteCustomer.getCustomerAddress().getCity() + concreteCustomer.getCustomerAddress().getStreet());
ConcreteCustomer concreteCustomer2 = (ConcreteCustomer)concreteCustomer.deepClone(); System.out.println(concreteCustomer2.getName() + "的地址:" + concreteCustomer2.getCustomerAddress().getProvince() + concreteCustomer2.getCustomerAddress().getCity() + concreteCustomer2.getCustomerAddress().getStreet()); }}
复制代码


编译并运行程序,输出如下结果:
张无忌的地址:省份二城市二街道二张无忌的地址:省份二城市二街道二
复制代码


发布于: 刚刚阅读数: 6
用户头像

代廉洁

关注

还未添加个人签名 2019.10.15 加入

还未添加个人简介

评论

发布
暂无评论
设计模式的艺术 第七章原型设计模式练习(在某销售管理系统中设计并实现了一个客户类Customer,其中包含一个名为客户地址的成员变量,客户地址的类型为Address。用浅克隆和深克隆分别实现Customer对象的复制)_设计模式的艺术_代廉洁_InfoQ写作社区