意图: 用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
结构图
实现示例:
原型Prototype:
public abstract class Prototype implements Cloneable {
public abstract void show();
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Prototype子类:
public class ConcretePrototype1 extends Prototype {
private String description;
public void setDescription(String description) {
this.description = description;
}
@Override
public void show() {
System.out.println("Concrete 1 description: " + description);
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object o = super.clone();
if (this.description == null) {
((ConcretePrototype1) o).description = this.description;
}
return o;
}
}
public class ConcretePrototype2 extends Prototype {
private int flag;
public void setFlag(int flag) {
this.flag = flag;
}
@Override
public void show() {
// TODO Auto-generated method stub
System.out.printf("Concrete 2 flag = %d\n", flag);
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object o = super.clone();
if (this.flag != 0) {
((ConcretePrototype2) o).flag = this.flag;
}
return o;
}
}
客户端使用:
import java.util.Enumeration;
import java.util.Vector;
public class Container {
private Vector vector;
public Container() {
vector = new Vector();
}
public void addItem(Prototype item) {
vector.addElement(item);
System.out.println("Now items list:");
Enumeration enumeration = vector.elements();
while (enumeration.hasMoreElements()) {
Prototype t = (Prototype)enumeration.nextElement();
t.show();
}
}
}
public class Client {
private Prototype prototype;
public void setPrototype(Prototype prototype) {
this.prototype = prototype;
}
public void runExample() throws Exception {
Container c = new Container();
ConcretePrototype1 item1 = null;
item1 = (ConcretePrototype1)prototype.clone();
item1.setDescription("this is item1");
c.addItem(item1);
ConcretePrototype1 item2 = null;
item2 = (ConcretePrototype1)item1.clone();
item2.setDescription("This is item2");
c.addItem(item2);
ConcretePrototype2 item3 = null;
item3 = new ConcretePrototype2();
item3.setFlag(100);
c.addItem(item3);
ConcretePrototype2 item4 = null;
item4 = (ConcretePrototype2)item3.clone();
item4.setFlag(500);
c.addItem(item4);
}
public static void main(String args[]) throws Exception {
Client client = new Client();
client.setPrototype(new ConcretePrototype1());
client.runExample();
}
}
执行结果:
Now items list:
Concrete 1 description: this is item1
Now items list:
Concrete 1 description: this is item1
Concrete 1 description: This is item2
Now items list:
Concrete 1 description: this is item1
Concrete 1 description: This is item2
Concrete 2 flag = 100
Now items list:
Concrete 1 description: this is item1
Concrete 1 description: This is item2
Concrete 2 flag = 100
Concrete 2 flag = 500
注:示例中原型Prototype只有一个属性,通过复制来创建没有体现其优势,当prototype中属性较多时,通过复制可以节省初始化的开销
- 大小: 35.3 KB
分享到:
相关推荐
设计模式之原型模式prototype有更多需要请联系我wyb_jany@126.com
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
主要介绍了PHP设计模式:原型模式Prototype,结合实例形式详细分析了PHP原型模式Prototype的基本概念、功能、原理、实现方法与操作注意事项,需要的朋友可以参考下
设计模式C++学习之原型模式(Prototype)
即原型模式,提供一个已经存在的对象进行新对象创建的接口,一般情况下都是使用Clone接口。 此模式非常简单,简单的说就是复制多个当前对象供使用。Prototype模式允许一个对象再创建另外一个可定制的对象,根本...
6、原型模式(Prototype Pattern) 用意:通过拷贝创建一个新的对象
原型模式的示例代码和文档,学习原型模式的参考资料。
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/1032892
一个原型设计模式的应用案例,C#语言编写,有助于设计模式的理解和学以致用。
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
原型模式是一种创建型设计模式,它通过复制一个现有的对象来创建新的对象,而不是通过调用构造函数的方式。这种方式可以在运行时动态地创建和修改对象,而不需要知道具体的创建细节 。 原型模式的基本概念包括以下...
3.设计模式Design Pattern:创建型模式(厂模式Factory、抽象工厂模式Abstract Factory、单例模式Singleton、建造者模式Builder、原型模式Prototype和对象池模式Object Pool Pattern)、结构型模式(适配器模式、...
IOS设计模式浅析之原型模式(Prototype)--copy - iOS知识库1
NULL 博文链接:https://gary0416.iteye.com/blog/904124
NULL 博文链接:https://jacky-dai.iteye.com/blog/2295379
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当...
C#面向对象设计模式纵横谈(6):Prototype 原型模式(创建型模式) 体验课程
概述 原型模式是指原型实例指向创建对象的种类,并通过拷贝这些原型创建新的对象,是一种用来创建对象的模式,也就是创建一个对象作为另一个对象的prototype属性; prototype警告:学习了解原型模式前需先学习...
prototype原型模式,通过拷贝构造函数,实现复制一个与现有对象一样的对象