Java中的原型设计模式

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<String> empList;
	
	public Employees(){
		empList = new ArrayList<String>();
	}
	
	public Employees(List<String> 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<String> getEmpList() {
		return empList;
	}

	@Override
	public Object clone() throws CloneNotSupportedException{
			List<String> temp = new ArrayList<String>();
			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<String> list = empsNew.getEmpList();
		list.add("John");
		List<String> 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中的原型设计模式。

翻译自: https://www.journaldev.com/1440/prototype-design-pattern-in-java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值