Skip to content

Commit ed61257

Browse files
committed
设计模式-原型模式
1 parent bc54547 commit ed61257

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.design.prototype;
2+
3+
/**
4+
* 地址
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/1 17:32
8+
*/
9+
public class Location implements Cloneable {
10+
11+
private StringBuffer address;
12+
private String remark;
13+
14+
public Location(StringBuffer address, String remark) {
15+
this.address = address;
16+
this.remark = remark;
17+
}
18+
19+
@Override
20+
protected Object clone() throws CloneNotSupportedException {
21+
// String基础属性可以不用处理,但是StringBuffer等引用对象需要处理
22+
Location location = (Location) super.clone();
23+
location.setAddress(new StringBuffer(address));
24+
return location;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "Location{" +
30+
"address=" + address +
31+
", remark='" + remark + '\'' +
32+
'}';
33+
}
34+
35+
public StringBuffer getAddress() {
36+
return address;
37+
}
38+
39+
public void setAddress(StringBuffer address) {
40+
this.address = address;
41+
}
42+
43+
public String getRemark() {
44+
return remark;
45+
}
46+
47+
public void setRemark(String remark) {
48+
this.remark = remark;
49+
}
50+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.design.prototype;
2+
3+
/**
4+
* 原型模式 - 克隆,拷贝
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/1 17:32
8+
*/
9+
public class Main {
10+
11+
public static void main(String[] args) throws Exception {
12+
StringBuffer address = new StringBuffer("深圳市南山区XXX");
13+
Location location = new Location(address, "XXX村");
14+
Person wangMing = new Person("李明", 20, location);
15+
// 克隆wangMing
16+
Person liMing = (Person) wangMing.clone();
17+
// 修改原始对象
18+
wangMing.setName("王明");
19+
wangMing.setAge(30);
20+
address.reverse();
21+
location.setRemark("XXX路");
22+
// 输出原始和克隆对象,正常应该是深拷贝
23+
// 修改原始对象,克隆对象还是刚开始声明的属性
24+
// 将Person重写的clone方法对Location的克隆注释放开
25+
// 以及Location重写的clone方法对StringBuffer的克隆注释放开
26+
// 不然就会出现浅拷贝的问题
27+
System.out.println(wangMing);
28+
System.out.println(liMing);
29+
}
30+
31+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.design.prototype;
2+
3+
/**
4+
* 人,姓名,年龄,地址
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/1 17:32
8+
*/
9+
public class Person implements Cloneable {
10+
11+
private String name;
12+
private int age;
13+
private Location location;
14+
15+
public Person(String name, int age, Location location) {
16+
this.name = name;
17+
this.age = age;
18+
this.location = location;
19+
}
20+
21+
@Override
22+
protected Object clone() throws CloneNotSupportedException {
23+
Person person = (Person) super.clone();
24+
// 基础属性String及int可以不用处理
25+
// 内置引用不重新进行克隆是浅拷贝,会导致指向同一个Location
26+
// 必须针对内部引用再次进行克隆,这样才是深拷贝
27+
person.setLocation((Location) location.clone());
28+
return person;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "Person{" +
34+
"name='" + name + '\'' +
35+
", age=" + age +
36+
", location=" + location +
37+
'}';
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public void setName(String name) {
45+
this.name = name;
46+
}
47+
48+
public int getAge() {
49+
return age;
50+
}
51+
52+
public void setAge(int age) {
53+
this.age = age;
54+
}
55+
56+
public Location getLocation() {
57+
return location;
58+
}
59+
60+
public void setLocation(Location location) {
61+
this.location = location;
62+
}
63+
}

0 commit comments

Comments
 (0)