• Java原型模式


    浅克隆:

    深克隆:

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

    1.对任何的对象x,都有x.clone() != x,即克隆对象与愿对象不是同一个对象。

    2.对任何的对象x,都有x.clone().getClass == x.getClass(),即克隆对象与原对象的类型一样。

    3.如果对象x的equals()方法定义恰当,那么x.clone().equals(x)应该成立。

    邮箱复制(浅克隆):

    Attachment:

    public class Attachment {
        public void download(){
            System.out.println("Download:");
        }
    }
    

    Email:

    public class Email implements Cloneable{
        Attachment attachment = null;
    
        public Email clone(){
            Object object = null;
            try{
                object = super.clone();
            }catch (CloneNotSupportedException e){
                System.err.println("Not support cloneable!");
                return null;
            }
            return (Email)object;
        }
    
        public void display(){
            System.out.println("i don't know what i need to display!");
        }
    
        public void setAttachment(Attachment attachment) {
            this.attachment = attachment;
        }
    
        public Attachment getAttachment() {
            return this.attachment;
        }
    }
    

    Client:

    public class Client {
        public static void main(String[] args){
            Email email = new Email();
            Attachment attachment = new Attachment();
            email.setAttachment(attachment);
            Email email_1;
            email_1 = email.clone();
            System.out.println("email 和 email_1 是否相同? " + (email == email_1));
            System.out.println("email 和 email_1 的类型是否相同? " + (email.getClass() == email_1.getClass()));
            System.out.println("email的附件 and email_1的附件是否相同? " + (email.getAttachment() == email_1.getAttachment()));
        }
    }
    

    Result:

    email 和 email_1 是否相同? false
    email 和 email_1 的类型是否相同? true
    email的附件 and email_1的附件是否相同? true

    邮箱复制(深克隆):通过序列化与反序列化进行深度克隆。

     

    Attachment:

    import java.io.Serializable;
    
    public class Attachment implements Serializable{
        public void download(){
            System.out.println("Download");
        }
    }
    

    Email:

    import java.io.*;
    
    public class Email implements Serializable{
        private Attachment attachment;
    
        public void setAttachment(Attachment attachment){
            this.attachment = attachment;
        }
    
        public Attachment getAttachment() {
            return this.attachment;
        }
    
        public void dislay(){
            System.out.println("...");
        }
    
        public Email deepClone() throws IOException,ClassNotFoundException,OptionalDataException{
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(this);
    
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
    
            return (Email)ois.readObject();
        }
    }
    

    Client:

    public class Client {
        public static void main(String[] args){
            Email email = new Email();
            Email email_1 = null;
            Attachment attachment = new Attachment();
            email.setAttachment(attachment);
            try {
                email_1 = email.deepClone();
            }catch (Exception e){
                System.err.println("deepClone failed!");
            }
            System.out.println("email 和 email_1 是否相同? " + (email == email_1));
            System.out.println("email 和 email_1 的类型是否相同? " + (email.getClass() == email_1.getClass()));
            System.out.println("email的附件 and email_1的附件是否相同? " + (email.getAttachment() == email_1.getAttachment()));
        }
    }
    

    Result:

    email 和 email_1 是否相同? false
    email 和 email_1 的类型是否相同? true
    email的附件 and email_1的附件是否相同? false

     

  • 相关阅读:
    第四章 基础命令的学习
    7-7命令总结
    第三章 虚拟机的简单使用及其xshell远程工具的使用
    第二章 虚拟机的软件及其安装
    在VMware中安装CentOS系统步骤
    Pytest02-用法和调用
    Pytest01-从官方文档开始学习
    浅谈mock技术
    C/C++ malloc、calloc、realloc和alloca
    Go 知识汇总
  • 原文地址:https://www.cnblogs.com/softwarecrash/p/8666810.html
Copyright © 2020-2023  润新知