设计模式--创建模式--原型模式

一、基本概念

  1、原型模式(Prototype模式)是指:用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象 。

      2、角色:

          A、抽象原型(Prototype)角色:此角色定义了的具体原型类所需的实现的方法,本例子直接精简为类。

          B、客户(Client)角色:客户类提出创建对象的请求;也就是我们用户使用复制粘贴的功能。

      3、其实原型模式的核心就是Prototype(抽象原型),他需要继承Cloneable接口,并且重写Object类中的clone方法即可。

二、简单例子

          A、抽象原型(Prototype)角色:

 1 package comm.pattern.create.prototype;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map.Entry;
 5 
 6 /**
 7  * 
 8  * @Title: ProtoTypeClass.java
 9  * @Package: comm.pattern.create.prototype
10  * @Description: 描述该文件做什么
11  * @author yangzhancheng
12  * @2020年3月25日:下午10:27:15
13  *
14  */
15 public class PersonProtoType implements Cloneable {
16 
17     // 年龄
18     private int age;
19 
20     // 姓名
21     private String name;
22 
23     // 朋友链
24     private HashMap<String, String> FriendsName = new HashMap<String, String>();
25 
26     // 朋友链
27     private HashMap<String, PersonProtoType> Friends = new HashMap<String, PersonProtoType>();
28 
29     public int getAge() {
30         return age;
31     }
32 
33     public void setAge(int age) {
34         this.age = age;
35     }
36 
37     public String getName() {
38         return name;
39     }
40 
41     public void setName(String name) {
42         this.name = name;
43     }
44 
45     public HashMap<String, String> getFriendsName() {
46         return FriendsName;
47     }
48 
49     public void setFriendsName(HashMap<String, String> friendsName) {
50         FriendsName = friendsName;
51     }
52 
53     public HashMap<String, PersonProtoType> getFriends() {
54         return Friends;
55     }
56 
57     public void setFriends(HashMap<String, PersonProtoType> friends) {
58         Friends = friends;
59     }
60 
61     @Override
62     public PersonProtoType clone() {
63         PersonProtoType clone = null;
64         try {
65             clone = (PersonProtoType)super.clone();
66         } catch (CloneNotSupportedException e) {
67             e.printStackTrace();
68         }
69         return clone;
70     }
71 
72     // 返回字符串
73     @Override
74     public String toString() {
75         StringBuffer strbuff = new StringBuffer();
76         strbuff.append("name:" + this.name);
77         strbuff.append(",age:" + this.age);
78 
79         for (Entry<String, String> entry : this.FriendsName.entrySet()) {
80             String columnName = entry.getKey();
81             String value = entry.getValue();
82             strbuff.append("\n第" + columnName + "个朋友;" + value);
83         }
84 
85         return strbuff.toString();
86     }
87 }
View Code

          B、客户(Client)角色:

 1 package comm.pattern.create.prototype;
 2 
 3 import java.util.HashMap;
 4 
 5 /**
 6  * 
 7  * @Title: Client.java
 8  * @Package: comm.pattern.create.prototype
 9  * @Description: 描述该文件做什么
10  * @author yangzhancheng
11  * @2020年3月25日:下午10:34:05
12  *
13  */
14 public class Client {
15 
16     public static void main(String[] args) {
17         
18         PersonProtoType person = new PersonProtoType();
19         person.setAge(24);
20         person.setName("丽丽");
21         HashMap<String, String> friendsName = new HashMap<String, String>();
22         friendsName.put("1", "小马");
23         friendsName.put("2", "小李");
24         person.setFriendsName(friendsName);
25         System.out.println("输出原来人物:\n"+person.toString());
26         
27         //克隆
28         System.out.println("---------------------------------------");
29         PersonProtoType coppyperson = person.clone();
30         System.out.println("输出复制人物:\n"+coppyperson.toString());
31         
32     }
33 
34 }
View Code

运行结果

输出原来人物:
name:丽丽,age:24
第1个朋友;小马
第2个朋友;小李
---------------------------------------
输出复制人物:
name:丽丽,age:24
第1个朋友;小马
第2个朋友;小李

三、总结

      注意:java执行clone方法的时候是直接从内存中去获取数据的,在第一次创建对象的时候就会把数据在内存保留一份,克隆的时候直接调用就好了,因此不初始化构造方法。

猜你喜欢

转载自www.cnblogs.com/fating/p/12571651.html