【设计模式】4、原型模式

代码示例:

 1 package com.shejimoshi.create.Prototype;
 2 
 3 
 4 /**
 5  * 功能:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
 6  * 适用:当一个系统应该独立于她得产品创建、构成和表示时,要使用Prototype模式
 7  *         实例化的类是在运行时刻指定的
 8  *         避免创建一个与产品类层次平行的工厂类层次时
 9  *         当一个类的实例只能有几个不同状态组合中的一种的时候
10  * 时间:2016年2月14日下午7:27:39
11  * 作者:cutter_point
12  */
13 public interface Prototype
14 {
15     //克隆自己
16     public Prototype Clone();
17     public void display();
18 }

实现接口:

 1 package com.shejimoshi.create.Prototype;
 2 
 3 
 4 /**
 5  * 功能:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
 6  * 适用:当一个系统应该独立于她得产品创建、构成和表示时,要使用Prototype模式
 7  *         实例化的类是在运行时刻指定的
 8  *         避免创建一个与产品类层次平行的工厂类层次时
 9  *         当一个类的实例只能有几个不同状态组合中的一种的时候
10  * 时间:2016年2月14日下午7:32:27
11  * 作者:cutter_point
12  */
13 public class Prototype1 implements Prototype
14 {
15     private String name;
16     private int id;
17     
18     //构造函数
19     public Prototype1(String name, int id)
20     {
21         this.name = name;
22         this.id =id;
23     }
24     
25     //拷贝构造函数
26     public Prototype1(Prototype1 p1)
27     {
28         this.name = p1.name;
29         this.id = p1.id;
30     }
31     
32     @Override
33     public Prototype Clone()
34     {
35         //拷贝自己,深拷贝还是浅拷贝,在我们的拷贝构造函数中定义
36         return new Prototype1(this);
37     }
38 
39     @Override
40     public void display()
41     {
42         System.out.println("Prototype1的名字和id是:" + this.name + " : " + this.id);
43     }
44 
45 }
 1 package com.shejimoshi.create.Prototype;
 2 
 3 
 4 /**
 5  * 功能:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
 6  * 适用:当一个系统应该独立于她得产品创建、构成和表示时,要使用Prototype模式
 7  *         实例化的类是在运行时刻指定的
 8  *         避免创建一个与产品类层次平行的工厂类层次时
 9  *         当一个类的实例只能有几个不同状态组合中的一种的时候
10  * 时间:2016年2月14日下午7:36:23
11  * 作者:cutter_point
12  */
13 public class Prototype2 implements Prototype
14 {
15     private String name;
16     private int id;
17     
18     //构造函数
19     public Prototype2(String name, int id)
20     {
21         this.name = name;
22         this.id =id;
23     }
24     
25     //拷贝构造函数
26     public Prototype2(Prototype2 p2)
27     {
28         this.name = p2.name;
29         this.id = p2.id;
30     }
31     
32     @Override
33     public Prototype Clone()
34     {
35         //拷贝自己,深拷贝还是浅拷贝,在我们的拷贝构造函数中定义
36         return new Prototype2(this);
37     }
38 
39     @Override
40     public void display()
41     {
42         System.out.println("Prototype2的名字和id是:" + this.name + " : " + this.id);
43     }
44 }

客户端,测试代码:

 1 package com.shejimoshi.create.Prototype;
 2 
 3 
 4 /**
 5  * 功能:客户端程序测试
 6  * 时间:2016年2月14日下午7:37:27
 7  * 作者:cutter_point
 8  */
 9 public class Test
10 {
11     public static void main(String args[])
12     {
13         Prototype obj1 = new Prototype1("cutter", 1);
14         Prototype obj2 = obj1.Clone();
15         Prototype obj3 = obj2.Clone();
16         
17         obj1.display();
18         obj2.display();
19         obj3.display();
20         
21         //类似的第二个对象
22         Prototype obj4 = new Prototype2("point", 2);
23         Prototype obj5 = obj4.Clone();
24         Prototype obj6 = obj5.Clone();
25         
26         obj4.display();
27         obj5.display();
28         obj6.display();
29 
30     }
31 }

输出结果:

Prototype1的名字和id是:cutter : 1
Prototype1的名字和id是:cutter : 1
Prototype1的名字和id是:cutter : 1
Prototype2的名字和id是:point : 2
Prototype2的名字和id是:point : 2
Prototype2的名字和id是:point : 2
原文地址:https://www.cnblogs.com/cutter-point/p/5189491.html