参考:https://blog.csdn.net/weixin_48052161/article/details/119493380
原型模式
定义;
原型模式属于对象创建模式,GOF 给它的定义为:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
Java中的原型模式:
在 Java 中提供了clone()方法来实现对象的克隆,所以Prototype 模式实现变得简单许 多。
原型模式分为
浅克隆
深克隆
代码演示:
浅克隆:
实现条件:
1. implements Cloneable接口
2. 重写clone()方法
location类(person内部的调用类):
package com.example.dtest.design23.prototype;
public 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;
}
}
person类:
package com.example.dtest.design23.prototype;
public class Person implements Cloneable {
int age = 8;
int score = 100;
Location loc = new Location("北京", 22);
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
测试类:
package com.example.dtest.design23.prototype;
public class PrototypeTest {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = null;
try {
p2 = (Person)p1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println(p2.age + "" + p2.score);
System.out.println(p2.loc);
System.out.println("----------------------");
System.out.println("p1==p2: "+(p1 == p2));
System.out.println("p1.loc == p2.loc: "+(p1.loc == p2.loc));
p1.loc.street = "上海";
System.out.println("p1.loc: "+p1.loc);
System.out.println("p2.loc: "+p2.loc);
}
}
可以看出 p2是有p1克隆过去的,且p1和p2.地址值不同,但成员属性相同
p1和p2的成员属性loc相同,都是指向堆中同一地址.即 p1.loc==p2.loc为true
所以当p1改变属性loc时,p2的属性loc也随之改变,这就是浅克隆
如果想克隆出来的p2不随p1改变而改变 ,则需要进行深克隆
深克隆:
location也继承了cloneable类:
package com.example.dtest.design23.prototype.deepPrototype;
public class Location implements Cloneable{
String street;
int roomNo;
public Location(String street,int roomNo){
this.street = street;
this.roomNo = roomNo;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Location{" +
"street='" + street + '\'' +
", roomNo=" + roomNo +
'}';
}
}
person类:
package com.example.dtest.design23.prototype.deepPrototype;
public class Person implements Cloneable{
int age = 8;
int score = 100;
Location loc = new Location("北京", 22);
@Override
protected Object clone() throws CloneNotSupportedException {
Person p = (Person) super.clone();
p.loc = (Location) loc.clone();
return p;
}
}
测试类:
package com.example.dtest.design23.prototype.deepPrototype;
public class DeepPrototypeTest {
public static void main(String[] args) {
Person p1 = new Person();
Person p2;
try {
p2 = (Person) p1.clone();
System.out.println(p2.age + "" + p2.score);
System.out.println(p2.loc);
System.out.println("--------------------");
System.out.println("p1==p2: "+(p1 == p2));
System.out.println("p1.loc==p2.loc: "+(p1.loc == p2.loc));
p1.loc.street = "上海";
System.out.println("p1.loc: "+p1.loc);
System.out.println("p2.loc: "+p2.loc);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
分析: 可以看出p1改变自己的属性loc 时,p2并不会随之变化