设计模式之原型模式

一 prototype

这里写图片描述

二 浅拷贝

obj对象的clone方法 继承Cloneable接口(空接口)是规范

public class Sheep implements Cloneable,Serializable {   //1997,英国的克隆羊,多利!
    private String sname;
    private Date birthday;
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object obj = super.clone();  //直接调用object对象的clone()方法!
        return obj;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public Sheep(String sname, Date birthday) {
        super();
        this.sname = sname;
        this.birthday = birthday;
    }
        public Sheep() {
    }
}

其中克隆后的对象指向同一个Date地址,其中一个date改变另外一个随之改变

三 深拷贝

public class Sheep2 implements Cloneable {   //1997,英国的克隆羊,多利!
    private String sname;
    private Date birthday;
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object obj = super.clone();  //直接调用object对象的clone()方法!
        //添加如下代码实现深复制(deep Clone)
        Sheep2 s = (Sheep2) obj;
        s.birthday = (Date) this.birthday.clone();  //把属性也进行克隆!
        return obj;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public Sheep2(String sname, Date birthday) {
        super();
        this.sname = sname;
        this.birthday = birthday;
    }
    public Sheep2() {
    }   
}

四 利用序列化和反序列化进行深拷贝

public class Client3 {
    public static void main(String[] args) throws CloneNotSupportedException, Exception {
        Date date = new Date(12312321331L);
        Sheep s1 = new Sheep("少利",date);
        System.out.println(s1);
        System.out.println(s1.getSname());
        System.out.println(s1.getBirthday());
//      使用序列化和反序列化实现深复制
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream    oos = new ObjectOutputStream(bos);
        oos.writeObject(s1);
        byte[] bytes = bos.toByteArray();
        ByteArrayInputStream  bis = new ByteArrayInputStream(bytes);
        ObjectInputStream     ois = new ObjectInputStream(bis);
        Sheep s2 = (Sheep) ois.readObject();   //克隆好的对象!
        System.out.println("修改原型对象的属性值");  
        date.setTime(23432432423L);
        System.out.println(s1.getBirthday());
        s2.setSname("多利");
        System.out.println(s2);
        System.out.println(s2.getSname());
        System.out.println(s2.getBirthday());   
    }
}

将原型模式和工厂模式结合起来:
这里写图片描述

以上几篇设计模式为创建型模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值