一、java中的构建模型直接支持原始模型。所有的javaBean都继承Object类,Object类有一个clone方法,用于克隆对象,而克隆对象又分为浅复制和深复制。

二、大致结构

1、原始接口类

2、具体原始类

三、代码

1、原始接口类

package com.prototype;

public interface Prototype extends Cloneable {
    
    Prototype deepClone();
    
    Prototype shallowClone();
    
}

2、具体原始类

package com.prototype;

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

public class Person implements Prototype, Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID = -5730267042366141834L;

    private String name;
    
    private int age;
    
    private Dog dog;
    
    public Person(String name, int age, Dog dog) {
        this.name = name;
        this.age = age;
        this.dog = dog;
        
    }
    
    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    
    /**
     * 深复制,会将dog引用的对象也复制一份
     */
    
    @Override
    public Prototype deepClone() {
        //创建数组输出流,将对象存到内存中
        ByteArrayOutputStream byteArrayOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        Person person = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            //将对象存入内存,并复制一份留在堆中
            objectOutputStream.writeObject(this);
            byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            //读取内存,将对象取出
            person = (Person) objectInputStream.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(byteArrayOutputStream != null) {
                    
                    byteArrayOutputStream.close();
                }
                
                if(objectOutputStream != null) {
                    
                    objectOutputStream.close();
                }
                
                if(byteArrayInputStream != null) {
                    
                    byteArrayInputStream.close();
                }

                if(objectInputStream != null) {
                    
                    objectInputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            
        }
        return person;
    }

    /**
     * 浅复制,复制dog对象的引用
     */
    @Override
    public Prototype shallowClone() {
        Prototype obj = null;
        try {
            obj = (Person)super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        
        return obj;
        
    }

        
        

}

3、Person的附属对象

package com.prototype;

import java.io.Serializable;

public class Dog implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 5518998591955476396L;

    private String name;
    
    private String color;
    
    public Dog(String name, String color) {
        this.name = name;
        
        this.color = color;
        
    }

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog [name=" + name + ", color=" + color + "]";
    }
    
    

}

4、测试类

package com.prototype;

public class Test {
    
    public static void main(String[] args) {
        
        Person person1 = new Person("youth", 22, new Dog("阿黄", "黄色"));
        
        Person person2 = (Person) person1.shallowClone();
        //浅复制的dog对象仍然是原来的对象
        System.out.println(person1.getDog() == person2.getDog());
        
        Person person3 = (Person) person1.deepClone();
        //深复制的dog对象是不是同一个对象,两者为克隆关系
        System.out.println(person1.getDog() == person3.getDog());
        System.out.println("本尊:" + person1.getDog());
        System.out.println("克隆:" + person3.getDog());
        
        
    }

}

5、结果

true
false
本尊:Dog [name=阿黄, color=黄色]
克隆:Dog [name=阿黄, color=黄色]