Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65fd2fc

Browse files
author
deeper
committed
complete prototype
1 parent e354649 commit 65fd2fc

File tree

6 files changed

+270
-3
lines changed

6 files changed

+270
-3
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.ruoxu.pattern.prototype.deep;
2+
3+
public class Address implements Cloneable{
4+
// 城市
5+
private String city;
6+
// 区
7+
private String district;
8+
// 街道
9+
private String street;
10+
11+
public Address(String city, String district, String street) {
12+
this.city = city;
13+
this.district = district;
14+
this.street = street;
15+
}
16+
17+
public String getCity() {
18+
return city;
19+
}
20+
21+
public void setCity(String city) {
22+
this.city = city;
23+
}
24+
25+
public String getDistrict() {
26+
return district;
27+
}
28+
29+
public void setDistrict(String district) {
30+
this.district = district;
31+
}
32+
33+
public String getStreet() {
34+
return street;
35+
}
36+
37+
public void setStreet(String street) {
38+
this.street = street;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "Address [city=" + city + ", district=" + district + ", street=" + street + "]";
44+
}
45+
46+
@Override
47+
protected Address clone(){
48+
try {
49+
Address address = (Address) super.clone();
50+
return address;
51+
} catch (CloneNotSupportedException e) {
52+
e.printStackTrace();
53+
return null;
54+
}
55+
}
56+
57+
58+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.ruoxu.pattern.prototype.deep;
2+
3+
/**
4+
* 从浅拷贝->深拷贝的变化就可以看出,只需把User中是【引用类型的成员变量】也实现Cloneable接口,
5+
* 重写clone方法,User的clone方法增加user.address = this.address.clone();即可。
6+
* 无论是User还是Address,实现clone方法都不需要管基本类型的数据和String类型,它们会自动拷贝。
7+
*
8+
* @author Limitless
9+
*
10+
*/
11+
public class Demo {
12+
public static void main(String[] args) {
13+
User origin = new User("李明",20,new Address("北京", "崇阳市", "柳州路"));
14+
System.out.println(origin);
15+
16+
User clone = origin.clone();
17+
System.out.println(clone);
18+
19+
clone.setName("东东");
20+
clone.setAge(80);
21+
clone.getAddress().setCity("上海");
22+
clone.getAddress().setDistrict("闵行区");
23+
clone.getAddress().setStreet("莘庄");
24+
25+
// clone.setAddress(new Address("上海", "闵行区", "莘庄")); // 注意这种方式设置是错的,setAddress是一个新的对象,所以不影响origin
26+
System.out.println(clone);
27+
28+
System.out.println(origin);
29+
30+
// 从运行结果可以看出,clone的Address改变后,origin的Address 并不会跟着改变(深拷贝)。
31+
32+
}
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.ruoxu.pattern.prototype.deep;
2+
3+
public class User implements Cloneable{
4+
private String name;
5+
private int age;
6+
private Address address;
7+
8+
public User(String name, int age, Address address) {
9+
super();
10+
this.name = name;
11+
this.age = age;
12+
this.address = address;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
23+
public int getAge() {
24+
return age;
25+
}
26+
27+
public void setAge(int age) {
28+
this.age = age;
29+
}
30+
31+
public Address getAddress() {
32+
return address;
33+
}
34+
35+
public void setAddress(Address address) {
36+
this.address = address;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "User [name=" + name + ", age=" + age + ", address=" + address + "]";
42+
}
43+
44+
@Override
45+
protected User clone() {
46+
try {
47+
User user = (User) super.clone();
48+
user.address = this.address.clone();
49+
return user;
50+
} catch (CloneNotSupportedException e) {
51+
e.printStackTrace();
52+
return null;
53+
}
54+
}
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.ruoxu.pattern.prototype.shallow;
2+
3+
public class Address {
4+
// 城市
5+
private String city;
6+
// 区
7+
private String district;
8+
// 街道
9+
private String street;
10+
11+
public Address(String city, String district, String street) {
12+
this.city = city;
13+
this.district = district;
14+
this.street = street;
15+
}
16+
17+
public String getCity() {
18+
return city;
19+
}
20+
21+
public void setCity(String city) {
22+
this.city = city;
23+
}
24+
25+
public String getDistrict() {
26+
return district;
27+
}
28+
29+
public void setDistrict(String district) {
30+
this.district = district;
31+
}
32+
33+
public String getStreet() {
34+
return street;
35+
}
36+
37+
public void setStreet(String street) {
38+
this.street = street;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "Address [city=" + city + ", district=" + district + ", street=" + street + "]";
44+
}
45+
46+
47+
}
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ruoxu.pattern.prototype;
1+
package com.ruoxu.pattern.prototype.shallow;
22

33
/**
44
* 原型模式
@@ -8,11 +8,31 @@
88
* 使用场景:如果类的初始化(构造函数)需要消耗很大的资源,如数据,硬件资源等,通过原型模式减少消耗,如果创建类的成本很小,那么就没必要使用此模式,直接new。
99
* 使用方法:要让实例调用clone方法就需要让此类实现Cloneable接口(同Serializable类似,Cloneable接口只是个标签接口,不含任何方法),一般如果你的子类没有特殊需要而重写clone()方法就直接用super.clone() 就行了(原因略)。
1010
* 注意事项:原型模式是在内存中二进制流的拷贝,要比直接new性能要好得多,但是正因如此,构造函数是不会执行的。
11-
* @author Administrator
12-
*
11+
* 当然不一定非要实现Closeable接口,还可以将对象串行化,implements Serializable,但是串行化却很耗时,这里仅仅实现第一种。
12+
*
13+
* @author limitless
1314
*/
1415
public class Demo {
1516
public static void main(String[] args) {
17+
User origin = new User("小明",20,new Address("北京", "崇阳市", "柳州路"));
18+
System.out.println(origin);
19+
20+
User clone = origin.clone();
21+
System.out.println(clone);
22+
23+
clone.setName("东东");
24+
clone.setAge(80);
25+
clone.getAddress().setCity("上海");
26+
clone.getAddress().setDistrict("闵行区");
27+
clone.getAddress().setStreet("莘庄");
28+
29+
// clone.setAddress(new Address("上海", "闵行区", "莘庄")); // 注意这种方式设置是错的,setAddress是一个新的对象,所以不影响origin
30+
System.out.println(clone);
31+
32+
System.out.println(origin);
33+
34+
// 从运行结果可以看出,clone的Address改变后,origin的Address也跟着改变(浅拷贝)。
35+
1636

1737
}
1838
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.ruoxu.pattern.prototype.shallow;
2+
3+
public class User implements Cloneable{
4+
private String name;
5+
private int age;
6+
private Address address;
7+
8+
public User(String name, int age, Address address) {
9+
super();
10+
this.name = name;
11+
this.age = age;
12+
this.address = address;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
23+
public int getAge() {
24+
return age;
25+
}
26+
27+
public void setAge(int age) {
28+
this.age = age;
29+
}
30+
31+
public Address getAddress() {
32+
return address;
33+
}
34+
35+
public void setAddress(Address address) {
36+
this.address = address;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "User [name=" + name + ", age=" + age + ", address=" + address + "]";
42+
}
43+
44+
@Override
45+
protected User clone() {
46+
try {
47+
User user = (User) super.clone();
48+
return user;
49+
} catch (CloneNotSupportedException e) {
50+
e.printStackTrace();
51+
return null;
52+
}
53+
}
54+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /