原型模式是一种创建型设计模式,它通过复制一个已经存在的实例来返回新的实例,而不是新建实例.被复制的实例就是我们所称的原型,这个原型是可定制的.
原型模式多用于创建复杂的或者耗时的实例, 因为这种情况下,复制一个已经存在的实例可以使程序运行更高效,或者创建值相等,只是命名不一样的同类数据.
原型模式中的拷贝分为"浅拷贝"和"深拷贝":
浅拷贝: 对值类型的成员变量进行值的复制,对引用类型的成员变量只复制引用,不复制引用的对象.
深拷贝: 对值类型的成员变量进行值的复制,对引用类型的成员变量也进行引用对象的复制.
类图:
实例一:浅拷贝
public class Prototype implements Cloneable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
public class TestMain {
public static void main(String[] args) {
testPrototype();
}
private static void testPrototype(){
Prototype pro = new Prototype();
pro.setName("original object");
Prototype pro1 = (Prototype)pro.clone();
pro.setName("changed object1");
System.out.println("original object:" + pro.getName());
System.out.println("cloned object:" + pro1.getName());
}
}
结果:
original object:changed object1
cloned object:original object
实例二: 浅拷贝
public class Prototype{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class NewPrototype implements Cloneable {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private Prototype prototype;
public Prototype getPrototype() {
return prototype;
}
public void setPrototype(Prototype prototype) {
this.prototype = prototype;
}
public Object clone(){
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
public class TestMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
testPrototype();
}
private static void testPrototype(){
Prototype pro = new Prototype();
pro.setName("original object");
NewPrototype newObj = new NewPrototype();
newObj.setId("test1");
newObj.setPrototype(pro);
NewPrototype copyObj = (NewPrototype)newObj.clone();
copyObj.setId("testCopy");
copyObj.getPrototype().setName("changed object");
System.out.println("original object id:" + newObj.getId());
System.out.println("original object name:" + newObj.getPrototype().getName());
System.out.println("cloned object id:" + copyObj.getId());
System.out.println("cloned object name:" + copyObj.getPrototype().getName());
}
}
结果:
original object id:test1
original object name:changed object
cloned object id:testCopy
cloned object name:changed object
实例三: 深拷贝
public class Prototype implements Cloneable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
public class NewPrototype implements Cloneable {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private Prototype prototype;
public Prototype getPrototype() {
return prototype;
}
public void setPrototype(Prototype prototype) {
this.prototype = prototype;
}
public Object clone(){
NewPrototype ret = null;
try {
ret = (NewPrototype)super.clone();
ret.prototype = (Prototype)this.prototype.clone();
return ret;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
testDeepCopy();
}
private static void testDeepCopy(){
Prototype pro = new Prototype();
pro.setName("original object");
NewPrototype newObj = new NewPrototype();
newObj.setId("test1");
newObj.setPrototype(pro);
NewPrototype copyObj = (NewPrototype)newObj.clone();
copyObj.setId("testCopy");
copyObj.getPrototype().setName("changed object");
System.out.println("original object id:" + newObj.getId());
System.out.println("original object name:" + newObj.getPrototype().getName());
System.out.println("cloned object id:" + copyObj.getId());
System.out.println("cloned object name:" + copyObj.getPrototype().getName());
}
}
结果:
original object id:test1
original object name:original object
cloned object id:testCopy
cloned object name:changed object
实例四: 利用串行化来做深复制
把对象写道流里的过程是串行化(Serilization)过程;把对象从流中读出来是并行化(Deserialization)过程. 写在流里的是对象的一个拷贝,然后再从流里读出来重建对象.
public class PrototypeSe implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class NewPrototypeSe implements Serializable {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private PrototypeSe prototype;
public PrototypeSe getPrototype() {
return prototype;
}
public void setPrototype(PrototypeSe prototype) {
this.prototype = prototype;
}
public Object deepClone(){
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(this);
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bi);
return oi.readObject();
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
public class TestDeepClone {
public static void main(String[] args) {
// TODO Auto-generated method stub
PrototypeSe po = new PrototypeSe();
po.setName("test1");
NewPrototypeSe se = new NewPrototypeSe();
se.setPrototype(po);
NewPrototypeSe deepClone = (NewPrototypeSe)se.deepClone();
deepClone.getPrototype().setName("test2");
System.out.println("original name:" + se.getPrototype().getName());
System.out.println("cloned name:" + deepClone.getPrototype().getName());
}
}
结果:
original name:test1
cloned name:test2
参考文章:http://www.iteye.com/topic/483469
相关推荐
.NET设计模式(6):原型模式(PrototypePattern)
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
原型模式(Prototype Pattern) 2 结构型模式 这些设计模式关注类和对象的组合。继承的概念被用来组合接口和定义组合对象获得新功能的方式。 适配器模式(Adapter Pattern) 桥接模式(Bridge Pattern) 过滤器模式...
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
原型模式是一种创建型设计模式,它通过复制一个现有的对象来创建新的对象,而不是通过调用构造函数的方式。这种方式可以在运行时动态地创建和修改对象,而不需要知道具体的创建细节 。 原型模式的基本概念包括以下...
用Java实现23种设计模式 1. 创建型模式 工厂模式(Factory Pattern) 抽象工厂模式(Abstract Factory Pattern) 单例模式(Singleton Pattern) 建造者模式(Builder Pattern) 原型模式(Prototype Pattern)...
(100%) 设计模式面面观(5):抽象工厂模式(AbstractFactory)-创建型模式 (100%) 设计模式面面观(6):生成器模式(Builder)-创建型模式 (100%) 设计模式面面观(7):原型模式(Prototype)-...
java设计模式之原型模式,希望对大家有所帮助。
原型模式(prototypePattern) 适配器模式(adapterPattern) 桥接模式(bridgePattern) 过滤器模式(filterPattern) 组合模式(compositePattern) 装饰器模式(decoratorPattern) 外观模式(facadePattern) 享元模式...
本文实例讲述了Python设计模式之原型模式。分享给大家供大家参考,具体如下: 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 一个原型模式的简单demo: #!/usr/bin/...
NULL 博文链接:https://jacky-dai.iteye.com/blog/2295379
1. 设计模式 1.1 含义 1.2 作用 1.3 设计原则 1.4 分类 2. 简单工厂模式 (SimpleFactoryPattern) 3. 工厂方法模式 (Factory Method) 4. 单例模式 (Singleton) 5. 抽象工厂模式 (Abstract Factory) 6. ...
3.4 prototype(原型)—对象创建型 模式 87 3.5 singleton(单件)—对象创建型 模式 84 3.6 创建型模式的讨论 89 第4章 结构型模式 91 4.1 adapter(适配器)—类对象结构型 模式 92 4.2 bridge(桥接)—...
原型模式(Prototype Pattern) 单例模式(Singleton Pattern) 结构型模式用来处理类或者对象的组合,主要包含以下7种设计模式: 适配器模式(Adapter Pattern) 桥接模式(Bridge Pattern) 组合...
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当...
Java设计模式书籍: 大话设计模式 Head First 设计模式 设计模式的类型: 总共有 23 种设计模式。这些模式可以分为三大类:创建型模式(Creational Patterns)、结构型模式(Structural Patterns)、 行为型模式...
23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm” 供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) ...
(二) 确保对象的唯一性——单例模式 (三) 确保对象的唯一性——单例模式 (四) 确保对象的唯一性——单例模式 (五) 原型模式-Prototype Pattern 对象的克隆——原型模式(一) 对象的克隆——原型模式(二) ...
适配器模式(Adapter Pattern) 提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式...