原型模式(未完成)

原型模式

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

原型模式结构图

  原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。

1.3.x 案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

public class Test {
public static void main(String[] args) {
//构建简历,可以设置基本信息和工作经历等
Resume a = new Resume("大鸟");
a.setPersonalInfo("男","29");
a.setWorkExperience("1998-2000", "XX公司");

Resume b = new Resume("大鸟");
b.setPersonalInfo("男","29");
b.setWorkExperience("1998-2000", "XX公司");

Resume c = new Resume("大鸟");
c.setPersonalInfo("男","29");
c.setWorkExperience("1998-2000", "XX公司");

a.display();
b.display();
c.display();


//原型模式
System.out.println("---------------------原型模式");
ResumeCloneable aNew = new ResumeCloneable("大鸟");
aNew.setPersonalInfo("男","29");
aNew.setWorkExperience("1998-2000", "XX公司");

ResumeCloneable bNew = (ResumeCloneable) aNew.clone();
bNew.setWorkExperience("1998-2006", "XX公司");

ResumeCloneable cNew = (ResumeCloneable) aNew.clone();
cNew.setPersonalInfo("男","24");

aNew.display();
bNew.display();
cNew.display();

//这样的实现仅仅是浅克隆,对于对象属性域需要更改结构为深克隆
}
}

public class Resume {
private String name;
private String sex;
private String age;
private String timeArea;
private String company;

public Resume(String name) {
this.name = name;
}

//设置个人信息
public void setPersonalInfo(String sex,String age){
this.sex = sex;
this.age = age;
}

//设置工作经历
public void setWorkExperience(String timeArea, String company){
this.timeArea = timeArea;
this.company = company;
}

//显式
public void display(){
System.out.println("{" + name + "} {" + sex + "} {" + age + "}");
System.out.println("工作经历:{" + timeArea + "} {" + company + "}");
}
}

public abstract class ICloneable {
public abstract Object clone();
}

public class ResumeCloneable extends ICloneable {
private String name;
private String sex;
private String age;
private String timeArea;
private String company;

public ResumeCloneable(String name) {
this.name = name;
}

public ResumeCloneable(String name, String sex, String age, String timeArea, String company) {
this.name = name;
this.sex = sex;
this.age = age;
this.timeArea = timeArea;
this.company = company;
}

//设置个人信息
public void setPersonalInfo(String sex,String age){
this.sex = sex;
this.age = age;
}

//设置工作经历
public void setWorkExperience(String timeArea, String company){
this.timeArea = timeArea;
this.company = company;
}

//显式
public void display(){
System.out.println("{" + name + "} {" + sex + "} {" + age + "}");
System.out.println("工作经历:{" + timeArea + "} {" + company + "}");
}

@Override
public Object clone() {
return (Object) this.memberwiseClone();
}
private ResumeCloneable memberwiseClone(){
//创建一个新的对象,然后将当前对象的非静态字段复制到该新对象。
//如果字段是值类型的,则对该字段执行逐位复制。
//如果字段是引用类型,则复制引用但不复制引用的对象;
//因此,原始对象及其副本引用同一对象
return new ResumeCloneable(this.name,this.sex,this.age,this.timeArea,this.company);
}
}

1.3.x 代码结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public abstract class Prototype {
private String id;

public Prototype(String id) {
this.id = id;
}

public String getId() {
return id;
}

public abstract Prototype clone();
}

public class ConcretePrototype1 extends Prototype {
public ConcretePrototype1(String id) {
super(id);
}

@Override
public Prototype clone() {
//创建当前对象的浅表副本
return (Prototype) this.memberwiseClone();
}

private ConcretePrototype1 memberwiseClone(){
//创建一个新的对象,然后将当前对象的非静态字段复制到该新对象。
//如果字段是值类型的,则对该字段执行逐位复制。
//如果字段是引用类型,则复制引用但不复制引用的对象;
//因此,原始对象及其副本引用同一对象
return new ConcretePrototype1(this.getId());
}
}

public class Main {
public static void main(String[] args) {
ConcretePrototype1 p1 = new ConcretePrototype1("I");
ConcretePrototype1 c1 = (ConcretePrototype1) p1.clone();
System.out.println("Cloned: {" + c1.getId() + "}");
}
}

参考:

🔗 《设计模式:可复用面向对象软件的基础》
🔗 《大话设计模式》