23种设计模式之原子模式

一、为什么有原型模式
       当一个类需要克隆的时候,我们总不希望new一个对象,然后逐个属性去设置值。 这个时候,我们亟需一种
  高效的对象copy方法,原型设计模式应运而生。
* 原型实现,在Person类中,实现了Cloneable 接口,然后重写clone方法,即可。
*
* 因为clone方法并不能穿透到son,也就是p和p1公用了son对象(p1对象对son的clone只是copy了指针,
* 并没有完全copy,也就是浅拷贝),所以p和p1同时输出的“隔壁老王的孩子”
*
* 深拷貝:
* 1)套娃式深拷贝
* 2)序列化深拷贝:
* 繼承Serializable接口

二:案列

  1 public class Person implements Cloneable{
  2 
  3     private String userName;
  4     private String sex;
  5     private int age;
  6     private Son son;
  7     
  8     public String getUserName() {
  9         return userName;
 10     }
 11     public void setUserName(String userName) {
 12         this.userName = userName;
 13     }
 14     public String getSex() {
 15         return sex;
 16     }
 17     public void setSex(String sex) {
 18         this.sex = sex;
 19     }
 20     public int getAge() {
 21         return age;
 22     }
 23     public void setAge(int age) {
 24         this.age = age;
 25     }
 26     
 27     public Son getSon() {
 28         return son;
 29     }
 30     public void setSon(Son son) {
 31         this.son = son;
 32     }
 33     
 34     @Override
 35     protected Person clone() throws CloneNotSupportedException {
 36       Person person = (Person) super.clone();
 37       person.setSon(person.getSon().clone());
 38       return person;
 39     }
 40     
 41     @Override
 42     public String toString() {
 43         return "Person [userName=" + userName + ", sex=" + sex + ", age=" + age + ", son=" + son + "]";
 44     }
 45 }
 46 
 47 public class Son implements Cloneable{
 48     
 49     private String sonName;
 50 
 51     public String getSonName() {
 52         return sonName;
 53     }
 54 
 55     public void setSonName(String sonName) {
 56         this.sonName = sonName;
 57     }
 58     
 59     @Override
 60     protected Son clone() throws CloneNotSupportedException{
 61         return (Son)super.clone();
 62     }
 63 
 64     @Override
 65     public String toString() {
 66         return "Son [sonName=" + sonName + "]";
 67     }
 68 }
 69 
 70 public static void main(String[] args) throws CloneNotSupportedException {
 71           Person p = new Person();
 72           p.setAge(19);
 73           p.setUserName("小明");
 74           p.setSex("男");
 75           Son son = new Son();
 76           son.setSonName("小明兒子");
 77           p.setSon(son);
 78           Person p1 = p.clone();
 79       
 80           System.out.println(p.hashCode());
 81           System.out.println(p1.hashCode());
 82      
 83           p.setAge(12);
 84           p1.getSon().setSonName("老王兒子");
 85      
 86           System.out.println(p);
 87           System.out.println(p1);
 88           
 89           //序列化深拷貝
 90 //          Person p = new Person();
 91 //          p.setAge(19);
 92 //          p.setUserName("小明");
 93 //          p.setSex("男");
 94 //          Son son = new Son();
 95 //          son.setSonName("小明的孩子");
 96 //          p.setSon(son);
 97 //  
 98 //          ByteArrayOutputStream bos=new ByteArrayOutputStream();
 99 //          ObjectOutputStream oos=new ObjectOutputStream(bos);
100 //          oos.writeObject(p);
101 //  
102 //          ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
103 //          ObjectInputStream ois=new ObjectInputStream(bis);
104 //          Person p1= (Person) ois.readObject();
105 //  
106 //  
107 //          System.out.println(p.hashCode());
108 //          System.out.println(p1.hashCode());
109 //  
110 //          p1.getSon().setSonName("隔壁老王的孩子");
111 //          p1.setAge(12);
112 //  
113 //          System.out.println(p);
114 //          System.out.println(p1);
115      }
原文地址:https://www.cnblogs.com/HYV587/p/13408008.html