声明:
本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/
/**
* Effective Java 建议使用copy constructor or copy factory来代替clone()方法:
* 1.public Product copy(Product p){}
* 2.public static Product newInstance(Product p){}
*/
public class PrototypePattern {
private Product product;
public PrototypePattern(Product product) {
this.product = product;
}
public void saveProduct() {
Product product2 = (Product) product.clone();
System.out.println("Product Name before save:" + product2.getName());
product2.setName("newProductName");
System.out.println("Product Name after save:" + product2.getName());
Label label2 = product2.getLabel();
label2.setName("newLabelName");
}
public static void main(String[] args) {
Label label = new Label();
label.setName("labelName");
Product product = new Product();
product.setName("productName");
product.setLabel(label);
PrototypePattern client = new PrototypePattern(product);
client.saveProduct();
System.out.println("original product name is " + product.getName());
System.out.println("original label name is " + product.getLabel().getName());
}
}
/*
* 1、java.lang.Cloneable是个空接口
* 如果你不实现这个接口的话,调用clone方法的时候会出现CloneNotSupportedException
* 2、clone()方法是Object的方法
*/
class Label implements Cloneable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object clone() {
Object object = null;
try {
object = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return object;
}
}
class Product implements Cloneable{
private Label label;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Label getLabel() {
return label;
}
public void setLabel(Label label) {
this.label = label;
}
//override Object的clone方法。手工克隆所有的field
public Product clone() {
Product p = new Product();
p.setName(name);
p.setLabel((Label)label.clone()); //深度克隆
return p;
}
/*
//也可这样写
public Object clone() {
Product other = null;
try {
//这一步是shallow copy。自动把name属性复制了。name是String类型,是"immutable object",否则就要像复制Label那样来复制
other = (Product)super.clone();
//下面的操作不可少,否则新旧的Product里面的Label是同一个
//而Label是mutable object,一个Product调用getLabel().setName()会影响到另一个Product
other.label = (Label) this.label.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return other;
}
*/
}
分享到:
相关推荐
研磨设计模式-配套源代码研磨设计模式-配套源代码
研磨设计模式-配套源代码.rar
研磨设计模式-配套源代码.7z
研磨设计模式第二版-配套源代码 UTF-8格式 研磨设计模式第二版-配套源代码 UTF-8格式
“1.1 设计模式是什么 1.1.1 什么是模式 从字面上理解,模,就是模型、模板的意思;式,就是方式、方法的意思。综合起来,所谓模式就是:可以作为模型或模板的方式或方法。... “研磨设计模式”。 iBooks.
设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。 使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写真正工程化;...
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...
1:本源代码是《研磨设计模式》一书的配套源代码 2:每个模式的示例源代码放在一个单独的文件夹下,以该模式的英文名称命名 3:每个模式下分成多个example,按照书的示例顺序分别命名为example1、example2.........
单例模式、工厂方法模式、策略模式、命令模式和桥接模式。
第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) ...
第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) ...
第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) ...
研磨设计模式的配套源码,请下载,不要想了,解压后,直接放到eclipse下面即可运行!
研磨设计模式 源代码 书上面的例子都在里面
研磨设计模式全部源代码,个人收集,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...
第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) ...
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...