博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中的原型设计模式
阅读量:2534 次
发布时间:2019-05-11

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

Prototype design pattern is one of the Creational Design pattern, so it provides a mechanism of object creation.

原型设计模式是创新设计模式之一,因此它提供了一种对象创建机制。

原型设计模式 (Prototype Design Pattern)

Prototype design pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing.

当创建对象的事务非常昂贵并且需要大量时间和资源并且您已经有一个相似的对象时,可以使用原型设计模式。

Prototype pattern provides a mechanism to copy the original object to a new object and then modify it according to our needs. Prototype design pattern uses java cloning to copy the object.

原型模式提供了一种机制,可以将原始对象复制到新对象,然后根据需要进行修改。 原型设计模式使用Java克隆来复制对象。

原型设计模式示例 (Prototype Design Pattern Example)

It would be easy to understand prototype design pattern with an example. Suppose we have an Object that loads data from database. Now we need to modify this data in our program multiple times, so it’s not a good idea to create the Object using new keyword and load all the data again from database.

用一个例子很容易理解原型设计模式。 假设我们有一个从数据库加载数据的对象。 现在我们需要在程序中多次修改此数据,因此使用new关键字创建Object并再次从数据库中加载所有数据不是一个好主意。

The better approach would be to clone the existing object into a new object and then do the data manipulation.

更好的方法是将现有对象克隆到新对象中,然后进行数据操作。

Prototype design pattern mandates that the Object which you are copying should provide the copying feature. It should not be done by any other class. However whether to use shallow or deep copy of the Object properties depends on the requirements and its a design decision.

原型设计模式要求您要复制的对象应提供复制功能。 不应由其他任何班级完成。 但是,使用对象属性的浅表副本还是深表副本取决于要求及其设计决策。

Here is a sample program showing Prototype design pattern example in java.

这是一个示例程序,显示了Java中的Prototype设计模式示例。

Employees.java

Employees.java

package com.journaldev.design.prototype;import java.util.ArrayList;import java.util.List;public class Employees implements Cloneable{	private List
empList; public Employees(){ empList = new ArrayList
(); } public Employees(List
list){ this.empList=list; } public void loadData(){ //read all employees from database and put into the list empList.add("Pankaj"); empList.add("Raj"); empList.add("David"); empList.add("Lisa"); } public List
getEmpList() { return empList; } @Override public Object clone() throws CloneNotSupportedException{ List
temp = new ArrayList
(); for(String s : this.getEmpList()){ temp.add(s); } return new Employees(temp); } }

Notice that the clone method is overridden to provide a deep copy of the employees list.

注意, clone方法被覆盖以提供雇员列表的深层副本。

Here is the prototype design pattern example test program that will show the benefit of prototype pattern.

这是原型设计模式示例测试程序,它将显示原型模式的好处。

PrototypePatternTest.java

PrototypePatternTest.java

package com.journaldev.design.test;import java.util.List;import com.journaldev.design.prototype.Employees;public class PrototypePatternTest {	public static void main(String[] args) throws CloneNotSupportedException {		Employees emps = new Employees();		emps.loadData();				//Use the clone method to get the Employee object		Employees empsNew = (Employees) emps.clone();		Employees empsNew1 = (Employees) emps.clone();		List
list = empsNew.getEmpList(); list.add("John"); List
list1 = empsNew1.getEmpList(); list1.remove("Pankaj"); System.out.println("emps List: "+emps.getEmpList()); System.out.println("empsNew List: "+list); System.out.println("empsNew1 List: "+list1); }}

Output of the above prototype design pattern example program is:

以上原型设计模式示例程序的输出为:

emps List: [Pankaj, Raj, David, Lisa]empsNew List: [Pankaj, Raj, David, Lisa, John]empsNew1 List: [Raj, David, Lisa]

If the object cloning was not provided, we will have to make database call to fetch the employee list every time. Then do the manipulations that would have been resource and time consuming.

如果未提供对象克隆,则我们将必须进行数据库调用以每次获取员工列表。 然后进行那些本来会很耗资源和时间的操作。

That’s all for prototype design pattern in java.

这就是Java中的原型设计模式。

翻译自:

转载地址:http://haqzd.baihongyu.com/

你可能感兴趣的文章
轮廓系数
查看>>
【Luogu1272】重建道路(动态规划)
查看>>
一次开发逻辑规范的总结
查看>>
Android 中 Movie 类显示GIF图片
查看>>
python学习---朴素贝叶斯算法的简单实现
查看>>
Eclipse使用技巧汇总
查看>>
RabbitMQ
查看>>
composite模式(摘自博客园博客)
查看>>
iOS程序执行顺序和UIViewController 的生命周期(整理)
查看>>
Embedding 文献收藏
查看>>
(笔试题)关于C++的虚函数和多态性
查看>>
蓝桥杯 问题 1110: 2^k进制数 (排列组合+高精度巧妙处理)
查看>>
初始python
查看>>
python3中文字符编码问题
查看>>
【转】贾扬清:希望Caffe成为深度学习领域的Hadoop
查看>>
php 加载字体 并保存成图片
查看>>
USACO 2016 January Contest Fort Moo (bzoj4506)
查看>>
Luffy之前端项目部署搭建
查看>>
string和byte[]的转换
查看>>
完成注册功能
查看>>