原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
简单描述:即通过实现接口Cloneable重写方法clone(),使得创建新的拷贝对象不需要一个成员一个成员的重新复制,而且可以提高创建对象的效率
Java中要想实现拷贝使用clone()方法,类必须实现Cloneable接口,并且重写Object类中的clone()方法,调用父类的clone()方法即可实现浅复制
代码如下:
WorkExperience类:
1 package com.longsheng.prototype;
2
3 public class WorkExperience {
4
5 private String workDate;
6 private String workCompany;
7
8 public WorkExperience(String workDate, String workCompany) {
9 this.workDate = workDate;
10 this.workCompany = workCompany;
11 }
12
13 public String getWorkDate() {
14 return workDate;
15 }
16
17 public void setWorkDate(String workDate) {
18 this.workDate = workDate;
19 }
20
21 public String getWorkCompany() {
22 return workCompany;
23 }
24
25 public void setWorkCompany(String workCompany) {
26 this.workCompany = workCompany;
27 }
28
29 }
Resume类:
1 package com.longsheng.prototype;
2
3 public class Resume implements Cloneable {
4
5 private String name;
6 private String age;
7 private String sex;
8 private WorkExperience mWorkExperience;
9
10 public Resume( String name ) {
11 this.name = name;
12 }
13
14 public String getName() {
15 return name;
16 }
17
18 public void setName(String name) {
19 this.name = name;
20 }
21
22 public String getAge() {
23 return age;
24 }
25
26 public void setAge(String age) {
27 this.age = age;
28 }
29
30 public String getSex() {
31 return sex;
32 }
33
34 public void setSex(String sex) {
35 this.sex = sex;
36 }
37
38 public WorkExperience getmWorkExperience() {
39 return mWorkExperience;
40 }
41
42 public void setmWorkExperience(WorkExperience mWorkExperience) {
43 this.mWorkExperience = mWorkExperience;
44 }
45
46 public void display() {
47 System.out.println(name + " " + sex + " " + age);
48 System.out.println(mWorkExperience.getWorkDate() + " " + mWorkExperience.getWorkCompany());
49 }
50
51 protected Object clone() throws CloneNotSupportedException {
52 return super.clone();
53 }
54
55 }
客户端:
1 package com.longsheng.prototype;
2
3 public class Client {
4
5 public static void main(String[] args) throws Exception {
6 Resume resumeA = new Resume("同学");
7 resumeA.setAge("25");
8 resumeA.setSex("男");
9 WorkExperience mWorkExperience = new WorkExperience("2011-2014", "XX公司");
10 resumeA.setmWorkExperience(mWorkExperience);
11 Resume resumeB = (Resume)resumeA.clone();
12 mWorkExperience.setWorkCompany("YY公司");
13 resumeA.display();
14 resumeB.display();
15 }
16
17 }
运行后得出结果:
1 同学 男 25
2 2011-2014 YY公司
3 同学 男 25
4 2011-2014 YY公司
由此可知super.clone()实现的是浅复制。
要想实现深复制,则可修改程序如下:
WorkExperience类(红色部分为修改的部分):
1 package com.longsheng.prototype;
2
3 public class WorkExperience implements Cloneable {
4
5 private String workDate;
6 private String workCompany;
7
8 public WorkExperience(String workDate, String workCompany) {
9 this.workDate = workDate;
10 this.workCompany = workCompany;
11 }
12
13 public String getWorkDate() {
14 return workDate;
15 }
16
17 public void setWorkDate(String workDate) {
18 this.workDate = workDate;
19 }
20
21 public String getWorkCompany() {
22 return workCompany;
23 }
24
25 public void setWorkCompany(String workCompany) {
26 this.workCompany = workCompany;
27 }
28
29 @Override
30 protected Object clone() throws CloneNotSupportedException {
31 return super.clone();
32 }
33
34 }
Resume类(红色部分为修改的部分):
1 package com.longsheng.prototype;
2
3 public class Resume implements Cloneable {
4
5 private String name;
6 private String age;
7 private String sex;
8 private WorkExperience mWorkExperience;
9
10 public Resume( String name ) {
11 this.name = name;
12 }
13
14 public String getName() {
15 return name;
16 }
17
18 public void setName(String name) {
19 this.name = name;
20 }
21
22 public String getAge() {
23 return age;
24 }
25
26 public void setAge(String age) {
27 this.age = age;
28 }
29
30 public String getSex() {
31 return sex;
32 }
33
34 public void setSex(String sex) {
35 this.sex = sex;
36 }
37
38 public WorkExperience getmWorkExperience() {
39 return mWorkExperience;
40 }
41
42 public void setmWorkExperience(WorkExperience mWorkExperience) {
43 this.mWorkExperience = mWorkExperience;
44 }
45
46 public void display() {
47 System.out.println(name + " " + sex + " " + age);
48 System.out.println(mWorkExperience.getWorkDate() + " " + mWorkExperience.getWorkCompany());
49 }
50
51 protected Object clone() throws CloneNotSupportedException {
52 Resume mResume = (Resume)super.clone();
53 mResume.setmWorkExperience((WorkExperience)this.getmWorkExperience().clone());
54 return mResume;
55 }//或者可以和书本类似,创建一个接收WorkExperience对象的构造器,在这个方法中先构造新对象,再设相应值,然后返回此新对象(如以下折叠的部分)
56
57 }
另外一种实现方法(Resume类)
1 package com.longsheng.prototype;
2
3 public class Resume implements Cloneable {
4
5 private String name;
6 private String age;
7 private String sex;
8 private WorkExperience mWorkExperience;
9
10 public Resume( String name ) {
11 this.name = name;
12 }
13
14 public Resume( WorkExperience mWorkExperience ) throws Exception {
15 this.mWorkExperience = (WorkExperience)mWorkExperience.clone();
16 }
17
18 public String getName() {
19 return name;
20 }
21
22 public void setName(String name) {
23 this.name = name;
24 }
25
26 public String getAge() {
27 return age;
28 }
29
30 public void setAge(String age) {
31 this.age = age;
32 }
33
34 public String getSex() {
35 return sex;
36 }
37
38 public void setSex(String sex) {
39 this.sex = sex;
40 }
41
42 public WorkExperience getmWorkExperience() {
43 return mWorkExperience;
44 }
45
46 public void setmWorkExperience(WorkExperience mWorkExperience) {
47 this.mWorkExperience = mWorkExperience;
48 }
49
50 public void display() {
51 System.out.println(name + " " + sex + " " + age);
52 System.out.println(mWorkExperience.getWorkDate() + " " + mWorkExperience.getWorkCompany());
53 }
54
55 protected Object clone() throws CloneNotSupportedException {
56 Resume mResume = null;
57 try {
58 mResume = new Resume(this.mWorkExperience);
59 mResume.setName(this.getName());
60 mResume.setAge(this.getAge());
61 mResume.setSex(this.getSex());
62 } catch (Exception e) {
63 e.printStackTrace();
64 }
65 return mResume;
66 }
67
68 }
Resume
客户端代码不变,运行后的结果:
1 同学 男 25
2 2011-2014 YY公司
3 同学 男 25
4 2011-2014 XX公司
大话设计模式_原型模式(Java代码),布布扣,bubuko.com
时间: 2024-12-26 19:19:10