序列化实现原型模式

Posted johnnyc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了序列化实现原型模式相关的知识,希望对你有一定的参考价值。

序列化实现原型模式

 Prototype 类中实现深度克隆(序列化与反序列化),实现 Cloneable,Serializable两接口。

package gupa.design.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Prototype implements Cloneable,Serializable{
    private String name;
    private PrototypeBo pb;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public PrototypeBo getPb() {
        return pb;
    }
    public void setPb(PrototypeBo pb) {
        this.pb = pb;
    }
    public Prototype(){}
    public Prototype(String name, PrototypeBo pb) {
        super();
        this.name = name;
        this.pb = pb;
    }
    @Override
    public String toString() {
        return "Prototype [name=" + name + ", pb=" + pb + "]";
    }
    public Object clone() throws CloneNotSupportedException {
        return this.deepclone();
    }
     
    public Object deepclone(){
        Prototype copy = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            copy = (Prototype)ois.readObject();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return copy;
    }
}

在引用对象上实现 Serializable 接口就行了。

package gupa.design.prototype;

import java.io.Serializable;

public class PrototypeBo implements Serializable{
    private String name;

    public String getName() {
        return name;
    }

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

    public PrototypeBo(){}
    public PrototypeBo(String name) {
        super();
        this.name = name;
    }

    @Override
    public String toString() {
        return "PrototypeBo [name=" + name + "]";
    }
//    public Object clone() {
//        PrototypeBo pb = null;
//        try {
//            ByteArrayOutputStream bos = new ByteArrayOutputStream();
//            ObjectOutputStream oos = new ObjectOutputStream(bos);
//            oos.writeObject(this);
//            
//            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
//            ObjectInputStream ois = new ObjectInputStream(bis);
//            pb = (PrototypeBo)ois.readObject();
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return pb;
//    }
}

测试

package gupa.design.prototype;

public class test {
    public static void main(String[] args) {
        Prototype p = new Prototype();
        p.setName("aaa");
        PrototypeBo pb = new PrototypeBo("abc");
        p.setPb(pb);
        Prototype p1 = null;
        try {
            p1 = (Prototype)p.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
//        PrototypeBo pb1 = (PrototypeBo)pb.clone();
        System.out.println(p==p1);
        System.out.println(p.getPb()==p1.getPb());
        System.out.println(p.hashCode()+"-----"+p1.hashCode());
        System.out.println(p.getPb().hashCode()+"-----"+p1.getPb().hashCode());
    }
}

实现效果:

false
false
1118140819-----1078694789
2101973421-----1831932724

 

以上是关于序列化实现原型模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式三 原型模式

序列化实现原型模式

创建型设计模式

单例模式之原型模式

设计模式-原型模式

Java设计模式—— 原型模式