设计模式-原型模式

原型模式-Prototype Pattern

原型模式(Prototype  Pattern):使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
原型模式是一种对象创建型模式。
抽象原型类:它是声明克隆方法的接口,是所有具体原型类的公共父类,可以是抽象类也可以是接口,甚至还可以是具体实现类。
  
具体原型类:它实现在抽象原型类中声明的克隆方法,在克隆方法中返回自己的一个克隆对象。
  
客户类:让一个原型对象克隆自身从而创建一个新的对象,在客户类中只需要直接实例化或通过工厂方法等方式创建一个原型对象,再通过调用该对象的克隆方法即可得到多个相同的对象。

在浅克隆中,如果原型对象的成员变量是值类型,将复制一份给克隆对象;如果原型对象的成员变量是引用类型,
      则将引用对象的地址复制一份给克隆对象,也就是说原型对象和克隆对象的成员变量指向相同的内存地址。
      在Java语言中,通过覆盖Object类的clone()方法可以实现浅克隆。

通用实现方法:

class ConcretePrototype implements Prototype
{
    private String  attr; //成员属性

    public void  setAttr(String attr)
    {
        this.attr = attr;
    }

    public String  getAttr()
    {
        return this.attr;
    }

    public Prototype  clone() //克隆方法
    {
        Prototype  prototype = new ConcretePrototype(); //创建新对象
    
        prototype.setAttr(this.attr);
    
        return prototype;
    }
}

Java语言提供的clone()方法:

class ConcretePrototype implements  Cloneable
{

    ……
    
    public Prototype  clone()
    {
    
      Object object = null;
    
      try {
    
         object = super.clone();
    
      } catch (CloneNotSupportedException exception) {
    
         System.err.println("Not support cloneable");
    
      }
    
      return (Prototype )object;
    }
    
    ……

}

深克隆:

public class WeeklyLog implements Serializable {
    private Attachment attachment;

    private String name;

    private String date;

    private String content;

    public void setAttachment(Attachment attachment) {

        this.attachment = attachment;

    }

    public void setName(String name) {

        this.name = name;

    }

    public void setDate(String date) {

        this.date = date;

    }

    public void setContent(String content) {

        this.content = content;

    }

    public Attachment getAttachment() {

        return (this.attachment);

    }

    public String getName() {

        return (this.name);

    }

    public String getDate() {

        return (this.date);

    }

    public String getContent() {

        return (this.content);

    }


    //使用序列化技术实现深克隆

    public WeeklyLog deepClone() throws IOException, ClassNotFoundException

    {

        //将对象写入流中

        ByteArrayOutputStream bao=new  ByteArrayOutputStream();

        ObjectOutputStream oos=new  ObjectOutputStream(bao);

        oos.writeObject(this);



        //将对象从流中取出

        ByteArrayInputStream bis=new  ByteArrayInputStream(bao.toByteArray());

        ObjectInputStream ois=new  ObjectInputStream(bis);

        return  (WeeklyLog)ois.readObject();

    }
}

来个例子:

抽象公文接口,也可以定义为抽象类,提供clone()方法的实现,将业务方法声明为抽象方法:

public interface OfficialDocument extends Cloneable
{
    public  OfficialDocument clone();

    public abstract void display();
}

公文实现类:

//可行性分析报告(Feasibility Analysis Report)类

class FAR implements OfficialDocument

{

    public  OfficialDocument clone(){
        OfficialDocument copy = null;
        try {
            copy = (OfficialDocument)super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return copy;
    }


    public  void display()
    {
        System.out.println("《可行性分析报告》");
    }

}

//软件需求规格说明书(Software Requirements Specification)类

class SRS implements OfficialDocument

{

    public  OfficialDocument clone(){
        OfficialDocument copy = null;
        try {
            copy = (OfficialDocument)super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return copy;
    }

    public  void display()
    {
        System.out.println("《软件需求规格说明书》");
    }

}

原型管理器:

//原型管理器(使用饿汉式单例实现)

import java.util.Hashtable;

class  PrototypeManager

{
    //定义一个Hashtable,用于存储原型对象

    private Hashtable ht=new Hashtable();

    private static PrototypeManager pm =  new PrototypeManager();

    //为Hashtable增加公文对象

    private  PrototypeManager()
    {
        ht.put("far",new  FAR());
        ht.put("srs",new  SRS());
    }


    //增加新的公文对象

    public void addOfficialDocument(String  key,OfficialDocument doc)

    {
        ht.put(key,doc);
    }



    //通过浅克隆获取新的公文对象

    public OfficialDocument  getOfficialDocument(String key)
    {
        return  ((OfficialDocument)ht.get(key)).clone();
    }



    public static PrototypeManager  getPrototypeManager()
    {
        return pm;
    }

}
Client类:

class Client

{

    public  static void main(String args[])

    {

        //获取原型管理器对象

        PrototypeManager pm =  PrototypeManager.getPrototypeManager();


        OfficialDocument  doc1,doc2,doc3,doc4;

        doc1  = pm.getOfficialDocument("far");

        doc1.display();

        doc2  = pm.getOfficialDocument("far");

        doc2.display();

        System.out.println(doc1  == doc2);



        doc3  = pm.getOfficialDocument("srs");

        doc3.display();

        doc4  = pm.getOfficialDocument("srs");

        doc4.display();

        System.out.println(doc3  == doc4);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值