首页 > 代码库 > 《大话设计模式》——读后感 (11)简历复印?——原型模式

《大话设计模式》——读后感 (11)简历复印?——原型模式

原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新对象[DP]

技术分享

 

说白了,原型模式还是属于创建型模式,主要功能还是创建新对象。下面我自己写一个原型模式试试:

案例1:自己写

原型接口(Prototy):

package com.sjmx.prototype.first;

public interface Prototy {
    
    public Object clone();
    
}

WorkExperience:

package com.sjmx.prototype.first;

public class WorkExperience implements Prototy{
    
    private String workDate;
    private String address;
    
    public String getWorkDate() {
        return workDate;
    }
    public void setWorkDate(String workDate) {
        this.workDate = workDate;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
    @Override
    public Object clone(){
        return new WorkExperience();
    }
    
    
}

 

ConPrototype:

package com.sjmx.prototype.first;

public class ConPrototype implements Prototy {
    
    public WorkExperience work;
    private String name;
    private int age;
    
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    public ConPrototype() {
        super();
        this.work = new WorkExperience();
    }
    
    
    public void setExperience(String workDate ,String address) {
        work.setAddress(address);
        work.setWorkDate(workDate);
    }
    
    @Override
    public Object clone(){
        ConPrototype con = new ConPrototype();
        con.work =    (WorkExperience) this.work.clone();
        return con;
    }
    
}

客户端调用:

package com.sjmx.prototype.first;

public class Client {
    
    public static void main(String[] args) {
        
        ConPrototype p = new ConPrototype();
        p.setAge(19);
        p.setName("java");
        p.setExperience("2014-02-12", "华晨科技");
        
        ConPrototype p2 = null;
        try {
            p2 = (ConPrototype) p.clone();
            p2.setExperience("2015-02-12", "隆正互联");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        ConPrototype p3 = null;
        try {
            p3 = (ConPrototype) p.clone();
            p3.setAge(13);
            p3.setName("C");
            p3.setExperience("2017-02-12", "苏南科技");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        System.out.println(p.getName());
        System.out.println(p.getAge());
        System.out.println(p.work.getWorkDate());
        System.out.println(p.work.getAddress());
        System.out.println("---------------------");
        
        System.out.println(p2.getName());
        System.out.println(p2.getAge());
        System.out.println(p2.work.getWorkDate());
        System.out.println(p2.work.getAddress());
        System.out.println("---------------------");
        
        System.out.println(p3.getName());
        System.out.println(p3.getAge());
        System.out.println(p3.work.getWorkDate());
        System.out.println(p3.work.getAddress());
 
    }
}

执行结果:

java
19
2014-02-12
华晨科技
---------------------
null
0
2015-02-12
隆正互联
---------------------
C
13
2017-02-12
苏南科技

分析:通过p和p2的比较, 我发现p2中的ConPrototype类信息没有值,而里面的内置对象WorkExperience却有数据,说明我模拟的原型全是是new出来的新对象,如果不提前设置值的话,会出现为空的情况;而再看看p3的打印结果,p3的值并没有覆盖之前p或p2设置好的值,说明我模拟的原型模式基本实现了深复制的功能,但却没有实现浅复制,必须要去设置人员name、age才行,这是bug;

 

案例2:jdk自带接口实现

WorkExperience:

package com.sjmx.prototype.sec;

public class WorkExperience implements Cloneable{
    
    private String workDate;
    private String address;
    
    public String getWorkDate() {
        return workDate;
    }
    public void setWorkDate(String workDate) {
        this.workDate = workDate;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
    @Override
    protected Object clone() throws CloneNotSupportedException{
        WorkExperience con =  (WorkExperience)super.clone();
        return con;
    }
    
    
}

ConPrototype:

package com.sjmx.prototype.sec;

public class ConPrototype implements Cloneable {
    
    public WorkExperience work;
    private String name;
    private int age;
    
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    public ConPrototype() {
        super();
        this.work = new WorkExperience();
    }
    
    
    public void setExperience(String workDate ,String address) {
        work.setAddress(address);
        work.setWorkDate(workDate);
    }
    
    @Override
    protected Object clone() throws CloneNotSupportedException{
        ConPrototype con =  (ConPrototype)super.clone();
        con.work =    (WorkExperience) this.work.clone();
        return con;
    }
    
}

运行结果:

java
19
2014-02-12
华晨科技
---------------------
java
19
2015-02-12
隆正互联
---------------------
C
13
2017-02-12
苏南科技

分析:1、通过p和p2的比较, 我发现p2中的ConPrototype类信息没有设置值,但是打印出来的却是有值的,而且和p里面的值一致,说明此接口实现浅复制

          2、再看看p3的打印结果,p3的值并没有覆盖之前p或p2设置好的值,说明使用jdk自带的接Cloneable也实现 了深复制:

 

 

 

《大话设计模式》——读后感 (11)简历复印?——原型模式