java设计模式(创建型)之原型模式
2014-03-09 23:08
483 查看
第0章:简介
原型模式定义:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。原型模式的实质:克隆生成对象
参考:http://chjavach.iteye.com/blog/1623968,研磨设计模式(书籍),大话设计模式(书籍)
模式图:
待补充
第1章:实践
第0节:自定义原型模式
(1)原型接口(Prototype.java)package com.mcc.core.designPattern.build.prototype.customPrototype; /** * 原型接口 * * @author <a href="mailto:417877417@qq.com">menergy</a> * DateTime: 14-3-9 下午4:59 */ public interface Prototype { /** * 克隆自身的方法 * @return */ public Prototype clone(); } |
package com.mcc.core.designPattern.build.prototype.customPrototype; /** * 产品类(自定义) * * @author <a href="mailto:417877417@qq.com">menergy</a> * DateTime: 14-3-9 下午5:04 */ public class Product implements Prototype { private String property1; private String property2; public String getProperty1() { return property1; } public void setProperty1(String property1) { this.property1 = property1; } public String getProperty2() { return property2; } public void setProperty2(String property2) { this.property2 = property2; } @Override public String toString() { return "Product{" + "property1='" + property1 + '\'' + ", property2='" + property2 + '\'' + '}'; } /** * 克隆自身,属性是值类型,属于浅度克隆 * @return */ @Override public Prototype clone() { Product product = new Product(); product.setProperty1(this.property1); product.setProperty2(this.property2); return product; } } |
package com.mcc.core.designPattern.build.prototype.customPrototype; /** * 目标克隆对象(自定义) * * 原型模式的定义:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。 * 原型模式的本质:克隆生成对象 * * @author <a href="mailto:417877417@qq.com">menergy</a> * DateTime: 14-3-9 下午5:06 */ public class PrototypeImpl implements Prototype { //值类型属性 private String property3; private String property4; //引用类型属性 private Product product; public String getProperty3() { return property3; } public void setProperty3(String property3) { this.property3 = property3; } public String getProperty4() { return property4; } public void setProperty4(String property4) { this.property4 = property4; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } @Override public String toString() { return "PrototypeImpl{" + "property1='" + product.getProperty1() + '\'' + "property2='" + product.getProperty2() + '\'' + "property3='" + property3 + '\'' + ", property4='" + property4 + '\'' + '}'; } /** * 对象克隆自身,含引用类型属性,采用深度克隆方式 * @return */ @Override public Prototype clone() { PrototypeImpl prototypeImpl = new PrototypeImpl(); prototypeImpl.setProperty3(this.property3); prototypeImpl.setProperty4(this.property4); //引用类型属性克隆 prototypeImpl.setProduct((Product)this.product.clone()); return prototypeImpl; } } |
package com.mcc.core.designPattern.build.prototype.customPrototype; /** * 客户端测试 * * @author <a href="mailto:417877417@qq.com">menergy</a> * DateTime: 14-3-9 下午5:20 */ public class Client { public static void main(String args[]){ //第一个实例 PrototypeImpl prototypeImpl1 = new PrototypeImpl(); Product product = new Product(); product.setProperty1("value1"); product.setProperty2("value2"); prototypeImpl1.setProduct(product); prototypeImpl1.setProperty3("value3"); prototypeImpl1.setProperty4("value4"); System.out.println("第一个实例:" + prototypeImpl1); //第二个实例,通过克隆获取 PrototypeImpl prototypeImpl2 = (PrototypeImpl)prototypeImpl1.clone(); //修改值 prototypeImpl2.getProduct().setProperty1("值1"); prototypeImpl2.setProperty3("值3"); System.out.println("第二个实例(克隆):" + prototypeImpl2); System.out.println("再看第一个实例(原实例):" + prototypeImpl1); } } |
第1节:java自带原型模式
(1)产品类(Product.java)package com.mcc.core.designPattern.build.prototype.javaPrototype; /** * 产品对象(Java自实现) * * @author <a href="mailto:417877417@qq.com">menergy</a> * DateTime: 14-3-9 下午5:04 */ public class Product implements Cloneable { private String property1; private String property2; public String getProperty1() { return property1; } public void setProperty1(String property1) { this.property1 = property1; } public String getProperty2() { return property2; } public void setProperty2(String property2) { this.property2 = property2; } @Override public String toString() { return "Product{" + "property1='" + property1 + '\'' + ", property2='" + property2 + '\'' + '}'; } /** * 克隆自身,属性是值类型,属于浅度克隆 * @return */ public Object clone() { Object obj = null; try { obj = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return obj; } } |
package com.mcc.core.designPattern.build.prototype.javaPrototype; /** * 目标克隆对象(Java自实现) * * 原型模式的定义:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。 * 原型模式的本质:克隆生成对象 * * @author <a href="mailto:417877417@qq.com">menergy</a> * DateTime: 14-3-9 下午5:06 */ public class PrototypeImpl implements Cloneable { //值类型属性 private String property3; private String property4; //引用类型属性 private Product product; public String getProperty3() { return property3; } public void setProperty3(String property3) { this.property3 = property3; } public String getProperty4() { return property4; } public void setProperty4(String property4) { this.property4 = property4; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } @Override public String toString() { return "PrototypeImpl{" + "property1='" + product.getProperty1() + '\'' + "property2='" + product.getProperty2() + '\'' + "property3='" + property3 + '\'' + ", property4='" + property4 + '\'' + '}'; } /** * 对象克隆自身,含引用类型属性,采用深度克隆方式 * @return */ public Object clone() { PrototypeImpl obj = null; try { obj = (PrototypeImpl)super.clone(); //这个很重要,引用类型自身克隆 obj.setProduct((Product)this.product.clone()); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return obj; } } |
package com.mcc.core.designPattern.build.prototype.javaPrototype; /** * 客户端测试 * * @author <a href="mailto:417877417@qq.com">menergy</a> * DateTime: 14-3-9 下午5:51 */ public class Client { public static void main(String args[]){ //第一个实例 PrototypeImpl prototypeImpl1 = new PrototypeImpl(); Product product = new Product(); product.setProperty1("value1"); product.setProperty2("value2"); prototypeImpl1.setProduct(product); prototypeImpl1.setProperty3("value3"); prototypeImpl1.setProperty4("value4"); System.out.println("第一个实例:" + prototypeImpl1); //第二个实例,通过克隆获取 PrototypeImpl prototypeImpl2 = (PrototypeImpl)prototypeImpl1.clone(); //修改值 prototypeImpl2.getProduct().setProperty1("值1"); prototypeImpl2.setProperty3("值3"); System.out.println("第二个实例(克隆):" + prototypeImpl2); System.out.println("再看第一个实例(原实例):" + prototypeImpl1); } } |
相关文章推荐
- JAVA设计模式(05):创建型-原型模式(Prototype)
- JAVA设计模式(5) —<创建型>原型模式(Prototype)
- Java[Android]设计模式创建型设计模式之Builder模式+单例模式+原型模式
- java设计模式——创建型之原型模式
- Java常用的设计模式05:常用设计模式之原型模式(创建型模式)
- JAVA设计模式(05):创建型-原型模式(Prototype)
- 设计模式(Design Pattern) - 创建型模式(Creational Pattern) - 原型模式(Prototype) - Java实现
- Java设计模式——创建型模式——原型模式(Prototype Pattern)
- JAVA设计模式(05):创建型-原型模式(Prototype)
- Java设计模式之创建型模式--原型模式
- Java设计模式_创建型_原型模式_复制对象及状态
- 【创建型模式】原型模式(Prototype)之23种java设计模式
- Java与设计模式(十四)创建型--原型模式
- Java设计模式(5)创建型:原型模式
- java-设计模式(创建型)-【原型模式】
- Java设计模式_(创建型)_原型模式
- Java技术_每天掌握一种设计模式(006)_使用场景及简单实例(创建型:原型模式)
- Java经典设计模式-创建型模式-原型模式(Prototype)
- java设计模式—原型模式
- 学习Java 23种设计模式详解笔记之创建型模式(一)