基本介绍
- 原型(Prototype)模式是指:用原型实例制定创建对象的种类,并且通过拷贝这些原型,创建新的对象。
- 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的细节。
- 工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建,即对象.clone()。
注意事项和细节
- 创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程,同时也能够提高效率。
- 不用重新初始化对象,而是动态地获取对象运行时的状态。
- 如果原始对象发生变化(增加或减少属性),其他克隆对象也会发生相应的变化,无需修改代码。
- 缺点:需要为每一个类配备一个克隆方法,这对全新的类来说不是很难,但对已有的类进行改造时,需要修改其源码,违背了ocp原则。
package com.fjh.prototype;
class Sheep implements Cloneable{
private String name;
private int age;
private String color;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//克隆该实例,使用默认的clone方法来完成
@Override
protected Object clone() {
Sheep sheep = null;
try{
sheep = (Sheep) super.clone();
}catch (Exception e){
System.out.println(e.getMessage());
}
return sheep;
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
}
public class PrototypeTest {
public static void main(String[] args){
Sheep sheep = new Sheep("yeah",5,"white");
Sheep sheep2 = (Sheep) sheep.clone();
Sheep sheep3 = (Sheep) sheep.clone();
Sheep sheep4 = (Sheep) sheep.clone();
System.out.println(sheep);
System.out.println(sheep2);
System.out.println(sheep3);
System.out.println(sheep4);
}
}
浅拷贝介绍
- 对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是该属性值复制一份给新的对象。
- 对于数据类型是引用数据类型的成员变量,比如说成员变量是某个数组、某个类的对象等,那么浅拷贝会进行引用传递,也就是只将该成员变量的引用值(内存地址)复制一份给新对象。因为实际上两个对象的该成员变量都指向同一个实例。在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成员变量值。
- 浅拷贝是使用默认的clone方法来实现的。
package com.fjh.prototype;
class Sheep implements Cloneable{
private String name;
private int age;
private String color;
public Sheep friend;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public void setFriend(Sheep friend) {
this.friend = friend;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//克隆该实例,使用默认的clone方法来完成
@Override
protected Object clone() {
Sheep sheep = null;
try{
sheep = (Sheep) super.clone();
}catch (Exception e){
System.out.println(e.getMessage());
}
return sheep;
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
}
public class PrototypeTest {
public static void main(String[] args){
Sheep sheep = new Sheep("yeah",5,"white");
sheep.setFriend(new Sheep("geap",4,"black"));
Sheep sheep2 = (Sheep) sheep.clone();
Sheep sheep3 = (Sheep) sheep.clone();
//浅拷贝
System.out.println(sheep +"------->"+sheep.friend.hashCode()); //Sheep{name='yeah', age=5, color='white'}------->930990596
System.out.println(sheep2 +"------->"+sheep2.friend.hashCode()); //Sheep{name='yeah', age=5, color='white'}------->930990596
System.out.println(sheep3 +"------->"+sheep3.friend.hashCode()); //Sheep{name='yeah', age=5, color='white'}------->930990596
}
}
深拷贝介绍
- 复制对象的所有基本数据类型的成员变量值。
- 为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员变量所引用的对象,直到该对象可达的所有对象,也就是说,对象进行深拷贝要对整个对象进行拷贝。
- 深拷贝实现方式1:重写clone方法来实现。
- 深拷贝实现方式2:通过对象序列化来实现。(推荐)
方式1:重写clone方法实现
package com.fjh.prototype;
class Sheep implements Cloneable{
private String name;
private int age;
private String color;
public Sheep friend;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public void setFriend(Sheep friend) {
this.friend = friend;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
protected Object clone() {
Sheep sheep = null;
try{
// 这里完成对基本数据类型(属性)和String的克隆
sheep = (Sheep) super.clone();
// 对引用类型的属性进行单独处理
sheep.friend = (Sheep) sheep.friend.clone();
}catch (Exception e){
System.out.println(e.getMessage());
}
return sheep;
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
}
public class PrototypeTest {
public static void main(String[] args){
Sheep sheep = new Sheep("yeah",5,"white");
sheep.setFriend(new Sheep("geap",4,"black"));
Sheep sheep2 = (Sheep) sheep.clone();
Sheep sheep3 = (Sheep) sheep.clone();
//深拷贝
System.out.println(sheep +"------->"+sheep.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->930990596
System.out.println(sheep2 +"------->"+sheep2.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->1921595561
System.out.println(sheep3 +"------->"+sheep3.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->565760380
}
}
方式2:通过序列化实现
package com.fjh.prototype;
import java.io.*;
class Sheep implements Serializable {
private String name;
private int age;
private String color;
public Sheep friend;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public void setFriend(Sheep friend) {
this.friend = friend;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Sheep deepClone(){
// 创建流对象
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
Sheep sheep = null;
try{
//序列化
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos); // 字节数组输入流转换成对象输出流
oos.writeObject(this); // 当前这个对象以对象流的方式输出
//反序列化
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
sheep = (Sheep) ois.readObject();
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
// 关闭流
try{
bos.close();
oos.close();
bis.close();
ois.close();
}catch (Exception e){
e.printStackTrace();
}
}
return sheep;
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
}
public class PrototypeTest {
public static void main(String[] args){
Sheep sheep = new Sheep("yeah",5,"white");
sheep.setFriend(new Sheep("geap",4,"black"));
Sheep sheep2 = sheep.deepClone();
Sheep sheep3 = sheep.deepClone();
//浅拷贝
System.out.println(sheep +"------->"+sheep.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->1268447657
System.out.println(sheep2 +"------->"+sheep2.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->110718392
System.out.println(sheep3 +"------->"+sheep3.friend.hashCode()); // Sheep{name='yeah', age=5, color='white'}------->425918570
}
}