博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【设计模式】4、原型模式
阅读量:4448 次
发布时间:2019-06-07

本文共 3293 字,大约阅读时间需要 10 分钟。

 

代码示例:

1 package com.shejimoshi.create.Prototype; 2  3  4 /** 5  * 功能:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 6  * 适用:当一个系统应该独立于她得产品创建、构成和表示时,要使用Prototype模式 7  *         实例化的类是在运行时刻指定的 8  *         避免创建一个与产品类层次平行的工厂类层次时 9  *         当一个类的实例只能有几个不同状态组合中的一种的时候10  * 时间:2016年2月14日下午7:27:3911  * 作者:cutter_point12  */13 public interface Prototype14 {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:2711  * 作者:cutter_point12  */13 public class Prototype1 implements Prototype14 {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     @Override33     public Prototype Clone()34     {35         //拷贝自己,深拷贝还是浅拷贝,在我们的拷贝构造函数中定义36         return new Prototype1(this);37     }38 39     @Override40     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:2311  * 作者:cutter_point12  */13 public class Prototype2 implements Prototype14 {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     @Override33     public Prototype Clone()34     {35         //拷贝自己,深拷贝还是浅拷贝,在我们的拷贝构造函数中定义36         return new Prototype2(this);37     }38 39     @Override40     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 Test10 {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 : 1Prototype1的名字和id是:cutter : 1Prototype1的名字和id是:cutter : 1Prototype2的名字和id是:point : 2Prototype2的名字和id是:point : 2Prototype2的名字和id是:point : 2

 

转载于:https://www.cnblogs.com/cutter-point/p/5189491.html

你可能感兴趣的文章
在ASP.NET页面中实现数据饼图
查看>>
在WPF中自定义控件(3) CustomControl (下)
查看>>
C# 验证识别基类
查看>>
用bat 删除当前文件夹下的某类文件
查看>>
先序遍历和后序遍历构建二叉树
查看>>
linux xorddos样本分析1
查看>>
【数论】-素数问题整理
查看>>
提高你的Java代码质量吧:正确使用String、StringBuffer、StringBuilder
查看>>
[happyctf]部分writeup
查看>>
HDU 1195 Open the Lock(BFS)
查看>>
Struts2的crud
查看>>
java上传文件
查看>>
大学生对技术网站需求的调查问卷结果分析
查看>>
测试一
查看>>
vertx的HttpServer模块
查看>>
as3事件流机制彻底理解
查看>>
Selenium webdriver操作日历控件
查看>>
Pascal程序练习-与7无关的数
查看>>
Linux:cut命令...未完待续
查看>>
PX4地面站QGroundControl在ubuntu下的安装
查看>>