设计模式(五)——原型模式

来源:互联网 发布:windows资源管理器 编辑:程序博客网 时间:2024/06/03 20:56

原型模式就是通过复制对象自身,从而克隆出多个与原型对象一模一样的对象。
原型模式包含三个组成:抽象原型类,具体原型类。
浅克隆与深克隆:
(1)浅克隆:在浅克隆中,被复制对象的所有普通成员变量都具有原来的对象的值,而所有的对其他对象的引用仍然指向原来的对象。换而言之,浅克隆仅仅复制所考虑的对象,而不复制它所引用的成员对象,也就是其中的成员对象并不复制。在浅克隆中,当对象被复制时,它所含的成员对象并没有被复制。
(2)深克隆:在深克隆中被复制对象的所有普通成员变量也都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不是原有的那些被引用的对象。换而言之就是深克隆把要复制的对象所引用的对象全部复制了一遍。

//浅克隆举例//具体原型类public class Email implements Cloneable{    private Attachment attachment = null;    public Email(){        this.attachment = new Attachment();    }    public Object clone(){        Email clone = null;        try{            clone = (Email)super.clone();        }        catch(CloneNotSupportedException e){            System.out.println("Clone failure");        }        return clone;    }    public Attachment getAttachment(){        return this.attachment;    }    public void display(){        System.out.println("查看邮件");    }}//辅助类public class Attachment {    public void download(){        System.out.println("下载附件");    }}
//深克隆举例//具体原型类import java.io.*;public class Email implements Serializable{    private Attachment attachment = null;    public Email(){        this.attachment = 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 Attachment getAttachment(){        return this.attachment;    }    public void display(){        System.out.println("查看邮件");    }}//辅助类import java.io.Serializable;public class Attachment implements Serializable {    public void download(){        System.out.println("下载附件");    }}

原型模式

//抽象原型类import java.util.*;public interface MyColor extends Cloneable{    public Object clone();    public void display();}//具体原型类public class Red implements MyColor {    public Object clone(){        Red r = null;        try{            r = (Red)super.clone();        }        catch(CloneNotSupportedException e){        }        return r;    }    @Override    public void display() {        // TODO Auto-generated method stub        System.out.println("this is red");    }}public class Blue implements MyColor {    public Object clone(){        Blue b = null;        try{            b = (Blue)super.clone();        }        catch(CloneNotSupportedException e){        }        return b;    }    @Override    public void display() {        // TODO Auto-generated method stub        System.out.println("this is blue");    }}//原型管理类import java.util.*;public class PrototypeManager {    private Hashtable ht = new Hashtable();    public PrototypeManager(){        ht.put("red", new Red());        ht.put("blue", new Blue());    }    public void addColor(String key, MyColor obj){        ht.put(key, obj);    }    public MyColor getColor(String key){        return (MyColor)((MyColor)ht.get(key)).clone();    }}
0 0
原创粉丝点击