每天一设计模式之原型模式
2009-06-01 18:17
197 查看
每天一设计模式之原型模式
原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
原型模式结构图:
深复制样例:
XieFei:
package com.test; public class XieFei { private String name = "xf";
private int id = 0; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public XieFei(String name, int id) {
this.name = name;
this.id = id;
}
}
MyClass:
package com.test; public class MyClass implements Cloneable {
public XieFei xiefei = new XieFei("xf", 2); @Override
protected Object clone() throws CloneNotSupportedException {
MyClass class1 = new MyClass();
return class1;
}
}
测试:
package com.test; public class Test { /**
* @param args
* @throws CloneNotSupportedException
*/
public static void main(String[] args) throws CloneNotSupportedException {
MyClass myclass = new MyClass();
myclass.xiefei.setId(1);
MyClass myclass2= (MyClass)myclass.clone();
myclass2.xiefei.setId(3);
System.out.println(myclass.xiefei.getId());
System.out.println(myclass2.xiefei.getId()); } }
相关文章推荐
- Java技术_每天掌握一种设计模式(006)_使用场景及简单实例(创建型:原型模式)
- 每天一个设计模式之 -- 原型模式
- 设计模式 (五)原型模式(Prototype)
- java设计模式——原型模式(Prototype Pattern)
- 设计模式之Prototype(原型)
- 设计模式-原型模式(Prototype)
- 原型设计模式
- 设计模式(9)-----原型模式
- 一起学设计模式 - 原型模式
- 学习设计模式-原型设计模式
- 每天学一点设计模式-抽象工厂模式
- 设计模式中的原型模式在Python程序中的应用示例
- 设计模式------PROTOTYPE(原型),TEMPLATE(模板)
- 小菜学习设计模式(四)—原型(Prototype)模式
- JAVA设计模式之原型模式
- Java设计模式-----Prototype原型模式
- 设计模式-原型模式
- 设计模式之原型模式(深入理解OC中的NSCopying协议以及浅拷贝、深拷贝)
- 可复用面向对象软件基础——设计模式(六)之原型模式
- php设计模式 原型模式