原型模式(prototype)
程序员文章站
2022-06-12 21:42:42
...
意图: 用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
结构图
[img]http://dl.iteye.com/upload/attachment/0066/4112/0ec018ac-09b2-3cf0-a91d-b47e6be9b45f.png[/img]
实现示例:
原型Prototype:
执行结果:
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中属性较多时,通过复制可以节省初始化的开销
结构图
[img]http://dl.iteye.com/upload/attachment/0066/4112/0ec018ac-09b2-3cf0-a91d-b47e6be9b45f.png[/img]
实现示例:
原型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中属性较多时,通过复制可以节省初始化的开销
上一篇: 原型与原型链