zoukankan      html  css  js  c++  java
  • 序列化实现原型模式

    序列化实现原型模式

     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
  • 相关阅读:
    Bank5
    面向对象特征之多态性
    继承性与super的使用练习
    阿里云服务器被挖矿minerd入侵的解决办法
    ES Pipeline Aggregation(管道聚合)
    Elasticsearch索引自动套用模板
    docker.service启动失败:Unit not found的原因及解决办法
    Kubernetes集群资源监控
    Kunbernetes-基于NFS的存储
    Kubernetes核心技术Helm
  • 原文地址:https://www.cnblogs.com/johnnyC/p/9595716.html
Copyright © 2011-2022 走看看