public class Prototype implements Serializable{
private static final long serialVersionUID = -463279279341581931L;
//浅复制
class BaseClass implements Cloneable{
private String str;
private Test test;
public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Test implements Serializable{
private static final long serialVersionUID = 6246981145613357962L;
private String strTest;
public String getStrTest() {
return strTest;
}
public void setStrTest(String strTest) {
this.strTest = strTest;
}
}//深复制
class DeepClone implements Serializable{
private static final long serialVersionUID = 7494636598162913845L;
private String str;
private Test test;
public Object clone(){
Object obj = null;
ByteArrayOutputStream bo = new ByteArrayOutputStream();
try {
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(this);
//若直接将byte[]数组传递过来,而这个byte数组不是使用ObjectOutputStream类写入的就会报错。
//问题解决的办法就是:用输出流得到byte[]数组。
byte[] b = bo.toByteArray(); //不能自己new一个byte
ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream oi = new ObjectInputStream(bi);
obj = oi.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return obj;
}
public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
public static void main(String[] args){
Prototype prototype = new Prototype();
BaseClass baseClass = prototype.new BaseClass();
Integer a = new Integer(3);
Integer b = new Integer(4);
Test test = prototype.new Test();
test.setStrTest("test");
// try {
// baseClass.setStr("abcde...");
// baseClass.setTest(test);
// BaseClass baseClass2 = (BaseClass) baseClass.clone();
// System.out.println(baseClass2.getStr());
// System.out.println(baseClass.getClass() == baseClass2.getClass());//同类型
// System.out.println(baseClass.equals(baseClass2));
// System.out.println(baseClass == baseClass2);
// System.out.println(a.getClass() == b.getClass());
// baseClass2.getTest().setStrTest("test2");
// System.out.println(baseClass.getTest().getStrTest());
// System.out.println(baseClass2.getTest().getStrTest());
// } catch (CloneNotSupportedException e) {
// e.printStackTrace();
// }
DeepClone deep = prototype.new DeepClone();
deep.setTest(test);
DeepClone deep2 = (DeepClone) deep.clone();
System.out.println("....");
System.out.println(deep2.getTest().getStrTest());
System.out.println(deep.getTest().getStrTest());
deep2.getTest().setStrTest("test2");
System.out.println(deep2.getTest().getStrTest());
System.out.println(deep.getTest().getStrTest());
}
}
分享到:
相关推荐
Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。...
设计模式C++学习之原型模式(Prototype)
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/1032892
NULL 博文链接:https://wy649898543.iteye.com/blog/1431591
IOS设计模式浅析之原型模式(Prototype)--copy - iOS知识库1
设计模式之原型模式prototype有更多需要请联系我wyb_jany@126.com
java设计模式之原型模式,希望对大家有所帮助。
设计模式之Prototype(原型) 设计模式之Builder 设计模式之Singleton(单态) 结构模式: 设计模式之Facade(外观) 设计模式之Proxy(代理) 设计模式之Adapter(适配器) 设计模式之Composite(组合) 设计模式之Decorator...
设计模式之Prototype(原型) 设计模式之Builder 设计模式之Singleton(单态) 结构模式: 设计模式之Facade(外观) 设计模式之Proxy(代理) 设计模式之Adapter(适配器) 设计模式之Composite(组合) 设计模式之Decorator...
这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高...
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
主要介绍了Java设计模式之原型模式(Prototype模式)介绍,本文讲解了如何使用原型模式并给出了代码实例,需要的朋友可以参考下
本文实例讲述了Python设计模式之原型模式。分享给大家供大家参考,具体如下: 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 一个原型模式的简单demo: #!/usr/bin/...
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
主要为大家详细介绍了Java设计模式之Prototype原型模式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
一个原型设计模式的应用案例,C#语言编写,有助于设计模式的理解和学以致用。
NULL 博文链接:https://jacky-dai.iteye.com/blog/2295379
创建模式:设计模式之Factory,设计模式之Prototype(原型),设计模式之Builder,设计模式之Singleton(单态). 结构模式:设计模式之Facade(外观),设计模式之Proxy(代理),设计模式之Adapter(适配器),设计模式之Composite...
C#面向对象设计模式纵横谈(6):Prototype 原型模式(创建型模式) 体验课程
原型模式是一种创建型设计模式,它通过复制一个现有的对象来创建新的对象,而不是通过调用构造函数的方式。这种方式可以在运行时动态地创建和修改对象,而不需要知道具体的创建细节 。 原型模式的基本概念包括以下...