java中23种设计模式之18-原型模式(Prototype pattern)

class Something
{
public String something=null;
}
class Prototype implements Cloneable
{
private String name;
public Something aSomething=new Something();
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public Prototype clone()
{
Prototype object=null;
try
{
object=(Prototype)super.clone();
}
catch(CloneNotSupportedException exception)
{
System.err.println("can not cloneable");
}
return object;
}
}

public class PrototypePattern
{
public static void main(String[] args)
{
Prototype aPrototype=new Prototype();
aPrototype.setName("real one");
aPrototype.aSomething.something="real something";
Prototype aCopyPrototype=aPrototype.clone();
aCopyPrototype.setName("clone one");
aPrototype.aSomething.something="clone something";
System.out.println("Prototype: "+aPrototype.getName()+",Something: "+aPrototype.aSomething.something);
System.out.println("Clone: "+aCopyPrototype.getName()+",Something: "+aCopyPrototype.aSomething.something);


}
}

posted on 2015-04-02 20:50  wudymand  阅读(130)  评论(0编辑  收藏  举报

导航