原型模式(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中属性较多时,通过复制可以节省初始化的开销
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值