Prototype模式
生活随笔
收集整理的這篇文章主要介紹了
Prototype模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原型模式創建對象不調用原對象的構造函數,是直接copy原對象的
淺克隆:對值類型的成員變量進行值的復制,對引用類型的成員變量只復制引用,不復制引用的對象.
深克隆:對值類型的成員變量進行值的復制,對引用類型的成員變量也進行引用對象的復制.
/**
* Created by marcopan on 17/10/20.
*/
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;
}
}
} /**
* Created by marcopan on 17/10/20.
*/
public class NewPrototype implements Cloneable {
private String id;
private Prototype prototype;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 static void main(String[] args) {
Prototype pro = new Prototype();
pro.setName("original object");
NewPrototype newObj = new NewPrototype();
newObj.setId("test1");
newObj.setPrototype(pro);
NewPrototype clonObj = (NewPrototype)newObj.clone();
clonObj.setId("testClone");
clonObj.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:" + clonObj.getId());
System.out.println("cloned object name:" + clonObj.getPrototype().getName());
}
}
運行結果:
淺克隆:對值類型的成員變量進行值的復制,對引用類型的成員變量只復制引用,不復制引用的對象.
深克隆:對值類型的成員變量進行值的復制,對引用類型的成員變量也進行引用對象的復制.
/**
* Created by marcopan on 17/10/20.
*/
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;
}
}
} /**
* Created by marcopan on 17/10/20.
*/
public class NewPrototype implements Cloneable {
private String id;
private Prototype prototype;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 static void main(String[] args) {
Prototype pro = new Prototype();
pro.setName("original object");
NewPrototype newObj = new NewPrototype();
newObj.setId("test1");
newObj.setPrototype(pro);
NewPrototype clonObj = (NewPrototype)newObj.clone();
clonObj.setId("testClone");
clonObj.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:" + clonObj.getId());
System.out.println("cloned object name:" + clonObj.getPrototype().getName());
}
}
運行結果:
original object id:test1
original object name:original object
cloned object id:testClone
cloned object name:changed object
轉載于:https://www.cnblogs.com/panning/p/7701136.html
總結
以上是生活随笔為你收集整理的Prototype模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [PA 2014]Kuglarz
- 下一篇: ASP.NET MVC中在 @Rende