原型模式

原型模式属于对象的创建模式。通过给出一个原型对象来指明所有创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象。

原型模式又分为浅拷贝和深拷贝模式,类中只有简单数据类型的拷贝属于浅拷贝,如:

 1 class Prototype implements Cloneable{
 2     private int id;
 3     private String name;
 4     public int getId() {
 5         return id;
 6     }
 7     public void setId(int id) {
 8         this.id = id;
 9     }
10     public String getName() {
11         return name;
12     }
13     public void setName(String name) {
14         this.name = name;
15     }
16     
17     public Object clone(){
18         Prototype p = null;
19         try {
20             p = (Prototype) super.clone();
21         } catch (CloneNotSupportedException e) {
22             e.printStackTrace();
23         }
24         return p;
25     }
26 }

当类中持有其他类的对象的实例时,需要使用深拷贝,深拷贝又有两种方法,第一种是将持有的对象再分别进行浅拷贝,这种方法适合持有少量对象:

 1 class Person implements Cloneable{
 2     private int id;
 3     private String name;
 4     public int getId() {
 5         return id;
 6     }
 7     public void setId(int id) {
 8         this.id = id;
 9     }
10     public String getName() {
11         return name;
12     }
13     public void setName(String name) {
14         this.name = name;
15     }
16     
17     public Object clone(){
18         Person person = null;
19         try{
20             person = (Person) super.clone();
21         }catch (CloneNotSupportedException e) {
22             e.printStackTrace();
23         }
24         return person;
25     }
26 }
27 
28 class Prototype implements Cloneable{
29     private boolean flag;
30     private Person person;
31     public boolean isFlag() {
32         return flag;
33     }
34     public void setFlag(boolean flag) {
35         this.flag = flag;
36     }
37     public Person getPerson() {
38         return person;
39     }
40     public void setPerson(Person person) {
41         this.person = person;
42     }
43     
44     public Object clone(){
45         Prototype p = null;
46         try{
47             p = (Prototype) super.clone();
48             p.person = (Person) person.clone();
49         }catch(CloneNotSupportedException e){
50             e.printStackTrace();
51         }
52         return p;
53     }
54 }

另一种深拷贝的方法就是使用序列化:

 1 import java.io.ByteArrayInputStream;
 2 import java.io.ByteArrayOutputStream;
 3 import java.io.IOException;
 4 import java.io.ObjectInputStream;
 5 import java.io.ObjectOutputStream;
 6 import java.io.Serializable;
 7 
 8 class Person implements Cloneable{
 9     private int id;
10     private String name;
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     
24     public Object clone(){
25         Person person = null;
26         try{
27             person = (Person) super.clone();
28         }catch (CloneNotSupportedException e) {
29             e.printStackTrace();
30         }
31         return person;
32     }
33 }
34 
35 class Prototype implements Serializable{
36     private boolean flag;
37     private Person person;
38     public boolean isFlag() {
39         return flag;
40     }
41     public void setFlag(boolean flag) {
42         this.flag = flag;
43     }
44     public Person getPerson() {
45         return person;
46     }
47     public void setPerson(Person person) {
48         this.person = person;
49     }
50     
51     public Object clone() {
52         try{
53             ByteArrayOutputStream bos = new ByteArrayOutputStream();
54             ObjectOutputStream oos = new ObjectOutputStream(bos);
55             oos.writeObject(oos);
56             
57             ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());  
58             ObjectInputStream ois = new ObjectInputStream(bis);  
59             return ois.readObject();
60         }catch(IOException e){
61             e.printStackTrace();
62         }catch(ClassNotFoundException e){
63             e.printStackTrace();
64         }
65         return null;
66     }
67 }

 

posted @ 2015-09-04 22:04  鬼神不灭  阅读(141)  评论(0编辑  收藏  举报