1.创建对象的方式有哪些?
a.new b。clone c。反序列化 d 反射
2.本节就考虑clone创建对象,原型模式简单来说就是利用clone创建对象,好处是,不用调用构造函数的的,clone是本地方法,速度很快,对于大对象创建的效率很棒。
3
1 package 原型模式; 2 3 import java.util.ArrayList; 4 5 class Student implements Cloneable 6 { 7 int id; 8 String name; 9 ArrayList arry; 10 public Student(int id,String name,ArrayList arry) 11 { 12 this.id=id; 13 this.name=name; 14 this.arry=arry; 15 16 } 17 public Student clone() throws CloneNotSupportedException 18 { 19 Student s= (Student) super.clone(); 20 // s.name=(String)this.name.clone(); 21 // s.arry=(ArrayList<Integer>) this.arry.clone(); 22 return s; 23 24 25 26 27 } 28 } 29 30 public class Pro { 31 public static void main(String args[]) throws CloneNotSupportedException 32 { 33 ArrayList<Integer> arr=new ArrayList<Integer>(); 34 arr.add(99);//语文 35 arr.add(100);//数学 36 Student s=new Student(1,"hansong",arr); 37 Student s2=s.clone(); 38 s2.arry.add(45); 39 System.out.println(s.arry);// 是多少呢? 结果是 99,100,45 40 41 42 43 44 45 46 47 } 48 49 }
3.深度拷贝
1 package 原型模式; 2 3 import java.util.ArrayList; 4 5 class Student implements Cloneable 6 { 7 int id; 8 String name; 9 ArrayList arry; 10 public Student(int id,String name,ArrayList arry) 11 { 12 this.id=id; 13 this.name=name; 14 this.arry=arry; 15 16 } 17 public Student clone() throws CloneNotSupportedException 18 { 19 Student s= (Student) super.clone(); 20 // s.name=(String)this.name.clone(); 21 s.arry=(ArrayList<Integer>) this.arry.clone(); 22 return s; 23 24 25 26 27 } 28 } 29 30 public class Pro { 31 public static void main(String args[]) throws CloneNotSupportedException 32 { 33 ArrayList<Integer> arr=new ArrayList<Integer>(); 34 arr.add(99);//语文 35 arr.add(100);//数学 36 Student s=new Student(1,"hansong",arr); 37 Student s2=s.clone(); 38 s2.arry.add(45); 39 System.out.println(s.arry);// 是多少呢? 结果是 99,100, 40 System.out.println(s2.arry);//99.100,45 41 42 43 44 45 46 47 48 } 49 50 }
4.list中存放对象的浅拷贝
1 package 原型模式; 2 3 import java.util.ArrayList; 4 class A 5 { 6 int x; 7 public A(int y) 8 { 9 x=y; 10 } 11 } 12 13 class Student implements Cloneable 14 { 15 int id; 16 String name; 17 ArrayList arry; 18 public Student(int id,String name,ArrayList arry) 19 { 20 this.id=id; 21 this.name=name; 22 this.arry=arry; 23 24 } 25 public Student clone() throws CloneNotSupportedException 26 { 27 Student s= (Student) super.clone(); 28 // s.name=(String)this.name.clone(); 29 s.arry=(ArrayList<A>) this.arry.clone(); 30 return s; 31 32 33 34 35 } 36 } 37 38 public class Pro { 39 public static void main(String args[]) throws CloneNotSupportedException 40 { 41 ArrayList<A> arr=new ArrayList<A>(); 42 arr.add(new A(1));//语文 43 arr.add(new A(2));//数学 44 Student s=new Student(1,"hansong",arr); 45 Student s2=s.clone(); 46 s2.arry.add(new A(-3)); 47 System.out.println(s.arry);// 是多少呢? 结果是 99,100, 48 System.out.println(s2.arry);//99.100,45 49 50 51 52 53 54 55 56 } 57 58 }
5存放对象的深度拷贝
1 package 原型模式; 2 3 import java.util.ArrayList; 4 class A implements Cloneable 5 { 6 int x; 7 public A(int y) 8 { 9 x=y; 10 } 11 public A clone() throws CloneNotSupportedException 12 { 13 return (A) super.clone(); 14 } 15 } 16 17 class Student implements Cloneable 18 { 19 int id; 20 String name; 21 ArrayList arry; 22 public Student(int id,String name,ArrayList arry) 23 { 24 this.id=id; 25 this.name=name; 26 this.arry=arry; 27 28 } 29 @SuppressWarnings("unchecked") 30 public Student clone() throws CloneNotSupportedException 31 { 32 Student s= (Student) super.clone(); 33 // s.name=(String)this.name.clone(); 34 s.arry=(ArrayList<A>) this.arry.clone(); 35 for(int i=0;i<s.arry.size();i++) 36 { 37 A a=(A) s.arry.get(i); 38 s.arry.set(i,a.clone()); 39 } 40 41 42 return s; 43 44 } 45 } 46 47 public class Pro { 48 public static void main(String args[]) throws CloneNotSupportedException 49 { 50 ArrayList<A> arr=new ArrayList<A>(); 51 arr.add(new A(1));//语文 52 arr.add(new A(2));//数学 53 Student s=new Student(1,"hansong",arr); 54 Student s2=s.clone(); 55 s2.arry.add(new A(-3)); 56 System.out.println(s.arry); 57 System.out.println(s2.arry); 58 59 60 61 62 63 64 65 } 66 67 }
[原型模式.A@1fb8ee3, 原型模式.A@61de33]
[原型模式.A@14318bb, 原型模式.A@ca0b6, 原型模式.A@10b30a7]