码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-04-02 22:24:14      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

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);


}
}

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

标签:

原文地址:http://www.cnblogs.com/wudymand/p/4388343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!