您的位置:首页 > 编程语言 > Java开发

设计模式读书笔记之原型模式(Prototype)

2010-03-22 20:33 260 查看
原型模式:通过克隆原型来创造新对象。

示例代码:

package designpattern.prototype;
public class Prototype implements Cloneable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Prototype(String name){
this.name = name;
}
public Prototype clone(){
Prototype p = null;
try {
p = (Prototype) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return p;
}

}

//test case
package designpattern.prototype;
public class Test {
public static void main(String[] args) {
Prototype p = new Prototype("I am a prototype");
Prototype p1 = p.clone();
System.out.println(p1.getName());
}
}


其实在java中原型模式其实就是Object.clone()的应用.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息