Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

设计模式——原型模式

$
0
0

今天我们来说说创建型模式的最后一种模式原型模式,我们先来说一下大家都熟悉的克隆羊多莉,它是一只通过现代工程创造出来的雌性绵羊,也是世界之初第一个成功克隆的人工动物。克隆是英文“clone”或“cloning”的音译,通常是利用生物技术由无性生殖产生与原个体有完全相同基因组织后代的过程。克隆我们了解了,那么原型是什么呢?什么事原型模式呢?其实原型跟我们的克隆是一样的。
原型模式的使用:通过new产生一个对象需要非常繁琐的数据准备或访问权限,可以使用原型模式。原型模式就是java中的克隆技术,以某个对象为原型,复制出新的对象。显然,新的对象具备原型对象的特点。优势就是:效率高,直接克隆,避免了重新执行构造过程的步骤。克隆类似于new但是却不同于new。new创建出来的对象属性采用的是默认值。克隆出来的对象的属性值则完全和原型对象的属性值相同。并且克隆出来的新对象改变不会影响原型对象。然后再修改克隆对象的值。我们复印东西想必大家都再熟悉不过了吧,那么原件其实就是我们的原型,复印出来的跟克隆出来的,想必大家都清楚了吧。
原型模式的实现:Cloneable接口课clone方法。原型模式中实现起来最困难的地方就是内存复制操作,不过还好,java中提供了clone()方法替我们做了很大一部分的工作。
克隆又分为深克隆和浅克隆,深克隆以后属性值是不能改变的,就像我们的身份证复印件是不能随便进行修改的。
上代码
下面的是浅克隆,属性值是可以进行修改的

public class IDCard implements Cloneable{
    public String name;
    public String address;
    public String id_number ;
    public Date validity;

    public IDCard(String name, String address, String id_number, Date validity) {
        this.name = name;
        this.address = address;
        this.id_number = id_number;
        this.validity = validity;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId_number() {
        return id_number;
    }

    public void setId_number(String id_number) {
        this.id_number = id_number;
    }

    public Date getValidity() {
        return validity;
    }

    public void setValidity(Date validity) {
        this.validity = validity;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

下面的是深克隆,属性值validity是不能进行修改的

public class IDCardDeep implements Cloneable{
    public String name;
    public String address;
    public String id_number ;
    public Date validity;

    public IDCardDeep(String name, String address, String id_number, Date validity) {
        this.name = name;
        this.address = address;
        this.id_number = id_number;
        this.validity = validity;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId_number() {
        return id_number;
    }

    public void setId_number(String id_number) {
        this.id_number = id_number;
    }
    public Date getValidity() {
        return validity;
    }

    public void setValidity(Date validity) {
        this.validity = validity;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        Object object = super.clone();
        IDCardDeep idCardDeep = (IDCardDeep) object;
       idCardDeep.validity = (Date) this.validity.clone();
        return idCardDeep;
    }
}

测试代码

 Date date =  new Date(61542297784720L);
        IDCard idCard = new IDCard("张三","北京市昌平区","110114199002141314",date);
        try {
            IDCard idCard1 = (IDCard) idCard.clone();
            System.out.println("idCard1-------->"+idCard1.getName());
            System.out.println("idCard1-------->"+idCard1.getAddress());
            System.out.println("idCard1-------->"+idCard1.getId_number());
            System.out.println("idCard1-------->"+idCard1.getValidity());
            date.setTime(61542297795720L);
            System.out.println("idCard1-------->"+idCard1.getName());
            System.out.println("idCard1-------->"+idCard1.getAddress());
            System.out.println("idCard1-------->"+idCard1.getId_number());
            System.out.println("idCard1-------->"+idCard1.getValidity());
            Date date1 =  new Date(61542297784720L);
            IDCardDeep idCardDeep = new IDCardDeep("张三","北京市昌平区","110114199002141314",date1);
            IDCardDeep idCardDeep1 = (IDCardDeep) idCardDeep.clone();
            System.out.println("idCardDeep1-------->"+idCardDeep1.getName());
            System.out.println("idCardDeep1-------->"+idCardDeep1.getAddress());
            System.out.println("idCardDeep1-------->"+idCardDeep1.getId_number());
            System.out.println("idCardDeep1-------->"+idCardDeep1.getValidity());
            date1.setTime(61542297795720L);
            System.out.println("idCardDeep1-------->"+idCardDeep1.getName());
            System.out.println("idCardDeep1-------->"+idCardDeep1.getAddress());
            System.out.println("idCardDeep1-------->"+idCardDeep1.getId_number());
            System.out.println("idCardDeep1-------->"+idCardDeep1.getValidity());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

打印的结果
这里写图片描述
到这里创建型模式已经完了,后面我将会继续写一些关于行为模式的。

作者:u014452224 发表于2017/2/14 11:11:43 原文链接
阅读:50 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>