原型设计模式-深浅拷贝问题

1. 原型模式动机与定义

1.1 原型模式动机

在软件系统中,有些对象创建过程较为复杂,而且有时候需要频繁的创建。原型模式通过给出一个原型对象来指明所要创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象。

1.2 原型模式定义

原型模式是一种创建型的设计模式,用原型实例制定创建对象的种类,并且通过复制这些原型创建新的对象。

2. 原型模式结构与分析

浅克隆
在这里插入图片描述
深克隆
在这里插入图片描述

2.1 原型模式结构

  • 抽象原型类(Prototype)

抽象原型类是定义具有克隆自己方法的接口,是所有具体原型类的公共父类是抽象类,也可以是接口

  • 具体原型类(ConcretePrototype)

具体原型类实现具体的克隆方法,在克隆方法中返回自己的一个克隆对象

  • 客户类(Client)

客户类让一个原型克隆自身,从而创建一个新的对象。

3. 浅克隆代码示例

具体原型

public class Email implements Cloneable{
    private Attachment attchment;

    public Email(){
        this.attchment = new Attachment();
    }

    public Object clone(){
        Email clone = null;

        try{
            clone = (Email) super.clone();
        }catch(CloneNotSupportedException c){
            c.printStackTrace();
        }

        return clone;
    }

    public Attachment getAttachment(){
        return this.attchment;
    }

    public void display(){
        System.out.println("查看邮件!");
    }

}

/**
 * 附件
 */
public class Attachment {
    
    public void download(){
        System.out.println("下载附件");
    }
}

客户端


/**
 * 浅拷贝客户端测试
 */
public class Client {
    public static void main(String[] args) {
        Email email, copyEmail;

        email = new Email();
        copyEmail = (Email) email.clone();

        System.out.println(email == copyEmail);
        System.out.println(email.getAttachment() == copyEmail.getAttachment());
    }
}
false
true

Java实现的clone方法只是浅克隆,它会将要克隆出一份对象的引用和克隆对象中非类对象属性,类对象不会对其复制

深克隆

具体原型

public class Email implements Cloneable, Serializable{
    private Attachment attchment;

    public Email(){
        this.attchment = new Attachment();
    }

    public Object deepClone() throws IOException, ClassNotFoundException, OptionalDataException {
        // 将对象写入流中
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bao);
        oos.writeObject(this);

        ByteArrayInputStream bis = new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (ois.readObject());
    }

    public Object clone(){
        Email clone = null;

        try{
            clone = (Email) super.clone();
        }catch(CloneNotSupportedException c){
            c.printStackTrace();
        }

        return clone;
    }

    public Attachment getAttachment(){
        return this.attchment;
    }

    public void display(){
        System.out.println("查看邮件!");
    }

}
/**
 * 附件
 */
public class Attachment implements Serializable {
    
    public void download(){
        System.out.println("下载附件");
    }
}

客户端

/**
 * 深拷贝客户端测试
 * 
 */
public class Client {
    public static void main(String[] args) throws OptionalDataException, ClassNotFoundException, IOException {
        Email email, copyEmail;

        email = new Email();
        copyEmail = (Email) email.deepClone();

        System.out.println(email == copyEmail);
        System.out.println(email.getAttachment() == copyEmail.getAttachment());
    }
}
false
false

深克隆是通过java io技术将对象写入流中,再输出到新的对象中

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李同学va

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值