原型模式(Prototype Pattern) 是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。
浅拷贝
/**
* 浅克隆
*/
public class Test {
public static void main(String[] args) throws Exception {
Person p1 = new Person();
Person p2 = (Person)p1.clone();
System.out.println(p2.age + " " + p2.score); // 8 100
System.out.println(p2.loc); // Location{street='bj', roomNo=22}
System.out.println(p1.loc == p2.loc); // true
p1.loc.street = "sh";
System.out.println(p2.loc); // Location{street='sh', roomNo=22}
// 修改的是p1的loc,p2的loc却跟着改变
}
}
class Person implements Cloneable {
int age = 8;
int score = 100;
Location loc = new Location("bj", 22);
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Location {
String street;
int roomNo;
@Override
public String toString() {
return "Location{" +
"street='" + street + '\'' +
", roomNo=" + roomNo +
'}';
}
public Location(String street, int roomNo) {
this.street = street;
this.roomNo = roomNo;
}
}
深拷贝
/**
* 深克隆的处理
*/
public class Test {
public static void main(String[] args) throws Exception {
Person p1 = new Person();
Person p2 = (Person)p1.clone();
System.out.println(p2.age + " " + p2.score); // 8 100
System.out.println(p2.loc); // Location{street='bj', roomNo=22}
System.out.println(p1.loc == p2.loc); // false
p1.loc.street = "sh";
System.out.println(p2.loc); // Location{street='bj', roomNo=22}
}
}
class Person implements Cloneable {
int age = 8;
int score = 100;
Location loc = new Location("bj", 22);
@Override
public Object clone() throws CloneNotSupportedException {
Person p = (Person)super.clone();
p.loc = (Location)loc.clone();
return p;
}
}
class Location implements Cloneable {
String street;
int roomNo;
@Override
public String toString() {
return "Location{" +
"street='" + street + '\'' +
", roomNo=" + roomNo +
'}';
}
public Location(String street, int roomNo) {
this.street = street;
this.roomNo = roomNo;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
String需要进一步深克隆吗?不需要
/**
* String需要进一步深克隆吗?不需要
*/
public class Test {
public static void main(String[] args) throws Exception {
Person p1 = new Person();
Person p2 = (Person)p1.clone();
System.out.println(p2.age + " " + p2.score); // 8 100
System.out.println(p2.loc); // Location{street='bj', roomNo=22}
System.out.println(p1.loc == p2.loc); // false
p1.loc.street = "sh";
System.out.println(p2.loc); // Location{street='bj', roomNo=22}
p1.loc.street.replace("sh", "sz");
System.out.println(p2.loc.street); // bj
}
}
class Person implements Cloneable {
int age = 8;
int score = 100;
Location loc = new Location("bj", 22);
@Override
public Object clone() throws CloneNotSupportedException {
Person p = (Person)super.clone();
p.loc = (Location)loc.clone();
return p;
}
}
class Location implements Cloneable {
String street;
int roomNo;
@Override
public String toString() {
return "Location{" +
"street='" + street + '\'' +
", roomNo=" + roomNo +
'}';
}
public Location(String street, int roomNo) {
this.street = street;
this.roomNo = roomNo;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
String需要进一步深克隆吗?其实这里是StringBuilder,所以需要
/**
* String需要进一步深克隆吗?其实这里是StringBuilder,所以需要
*/
public class Test {
public static void main(String[] args) throws Exception {
Person p1 = new Person();
Person p2 = (Person)p1.clone();
System.out.println("p1.loc == p2.loc? " + (p1.loc == p2.loc)); // p1.loc == p2.loc? false
p1.loc.street.reverse();
System.out.println(p2.loc.street); // jb
}
}
class Person implements Cloneable {
int age = 8;
int score = 100;
Location loc = new Location(new StringBuilder("bj"), 22);
@Override
public Object clone() throws CloneNotSupportedException {
Person p = (Person)super.clone();
p.loc = (Location)loc.clone();
return p;
}
}
class Location implements Cloneable {
StringBuilder street; // 这里是StringBuilder,不是String
int roomNo;
@Override
public String toString() {
return "Location{" +
"street='" + street + '\'' +
", roomNo=" + roomNo +
'}';
}
public Location(StringBuilder street, int roomNo) {
this.street = street;
this.roomNo = roomNo;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}