总体来说设计模式分为三大类:
创建型模式5种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
结构型模式7种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式11种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
原型模式
将一个对象作为原型,对其进行复制、克隆,产生一个和原对象类似的新对象
浅复制:将一个对象复制后,基本数据类型和String的变量都会重新创建,而引用类型除String外,指向的还是原对象所指向的。
深复制:将一个对象复制后,不论是基本数据类型还有引用类型,都是重新创建的。简单来说,就是深复制进行了完全彻底的复制,而浅复制不彻底。
Demo
浅复制
首先创建一个实体类
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;
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(){
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 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 TestMain {
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