设计模式系列:原型模式

概念

原型模式是特殊的创建模式,它创建对象不通过直接new的方式产生,而是通过已有的对象复制。

实现

浅拷贝

  • 类图:

图片

Product

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class Product implements Serializable {

private String name;

private String value;

private ProductB productB;

public ProductB getProductB() {
return productB;
}

public void setProductB(ProductB productB) {
this.productB = productB;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public void display() {
System.out.println("product name:" + this.name);
System.out.println("product value:" + this.value);
System.out.println("productB name:" + this.productB.getName());
}

public Product 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 (Product)oi.readObject();
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

}

ProductB

1
2
3
4
5
6
7
8
9
10
11
12
public class ProductB implements Serializable {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Client {

public static void main(String[] args) {
Product product = new Product();
ProductB productB = new ProductB();
productB.setName("B");
product.setName("X");
product.setValue("XX");
product.setProductB(productB);
Product clone = product.deepClone();
clone.getProductB().setName("newXX");
product.display();
clone.display();
}

}

执行结果:
Connected to the target VM, address: ‘127.0.0.1:58904’, transport: ‘socket’
product name:X
product value:XX
productB name:B
product name:X
product value:XX
productB name:newXX
Disconnected from the target VM, address: ‘127.0.0.1:58904’, transport: ‘socket’

深拷贝

  • 类图:

图片

Product

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class Product implements Cloneable {

private String name;

private String value;

private ProductB productB;

public ProductB getProductB() {
return productB;
}

public void setProductB(ProductB productB) {
this.productB = productB;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public void display() {
System.out.println("product name:" + this.name);
System.out.println("product value:" + this.value);
System.out.println("productB name:" + this.productB.getName());
}

public Product clone() {
try {
return (Product)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
}

ProductB

1
2
3
4
5
6
7
8
9
10
11
12
public class ProductB {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Client {

public static void main(String[] args) {
Product product = new Product();
ProductB productB = new ProductB();
productB.setName("B");
product.setName("X");
product.setValue("XX");
product.setProductB(productB);
Product clone = product.clone();
clone.getProductB().setName("newXX");
product.display();
clone.display();
}

}

执行结果:
Connected to the target VM, address: ‘127.0.0.1:58962’, transport: ‘socket’
product name:X
product value:XX
productB name:newXX
product name:X
product value:XX
productB name:newXX
Disconnected from the target VM, address: ‘127.0.0.1:58962’, transport: ‘socket’

场景

总结

浅拷贝 拷贝后的对象如果有嵌套的复杂对象,那么改变嵌套对象会跟着改变。只拷贝表层的对象信息。
深拷贝,是所有的都拷贝,包括嵌套对象。这里的实现是通过序列化的方式实现。