基本介绍
- 原型模式(Prototype模式)是指:用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象
- 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象, 无需知道如何创建的细节
- 工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建,即 对象.clone()
这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。
注意事项:与通过对一个类进行实例化来构造新对象不同的是,原型模式是通过拷贝一个现有对象生成新对象的。
深拷贝实现方式1:重写clone方法来实现深拷贝
深拷贝实现方式2:通过对象序列化实现深拷贝(推荐)
代码如下:
package com.longstudy.pattern.prototype;
import java.io.*;
/**
* @anthor longzx
* @create 2021 03 15 20:18
* @Description
**/
public class PrototypePatternDemo1 {
public static void main(String[] args) {
System.out.println("原型模式完成对象的创建");
// TODO Auto-generated method stub
Sheep sheep = new Sheep("tom", 1, "白色");
sheep.friend = new Sheep("jack", 2, "黑色");
sheep.master = new Person("老王",25);
//方式1
//Sheep sheep2 = (Sheep)sheep.clone(); //克隆
//Sheep sheep3 = (Sheep)sheep.clone(); //克隆
//Sheep sheep4 = (Sheep)sheep.clone(); //克隆
//Sheep sheep5 = (Sheep)sheep.clone(); //克隆
//方式2
Sheep sheep2 = (Sheep)sheep.deepClone(); //克隆
Sheep sheep3 = (Sheep)sheep.deepClone(); //克隆
Sheep sheep4 = (Sheep)sheep.deepClone(); //克隆
Sheep sheep5 = (Sheep)sheep.deepClone(); //克隆
System.out.println("sheep2 =" + sheep2 + "sheep2.friend=" + sheep2.friend.hashCode()+ "sheep2.master="+sheep2.master.hashCode());
System.out.println("sheep3 =" + sheep3 + "sheep3.friend=" + sheep3.friend.hashCode()+ "sheep3.master="+sheep3.master.hashCode());
System.out.println("sheep4 =" + sheep4 + "sheep4.friend=" + sheep4.friend.hashCode()+ "sheep4.master="+sheep4.master.hashCode());
System.out.println("sheep5 =" + sheep5 + "sheep5.friend=" + sheep5.friend.hashCode()+ "sheep5.master="+sheep5.master.hashCode());
}
}
class Sheep implements Serializable,Cloneable {
private String name;
private int age;
private String color;
private String address = "蒙古羊";
public Person master;//主人
public Sheep friend; //是对象, 克隆是会如何处理
public Sheep(String name, int age, String color) {
super();
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; }
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
", address='" + address + '\'' +
", master=" + master +
", friend=" + friend +
'}';
}
//克隆该实例,使用默认的clone方法来完成
@Override
protected Object clone() {
Sheep sheep = null;
try {
sheep = (Sheep)super.clone();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
// TODO Auto-generated method stub
if (this.friend !=null){ //实现链表复制 实现深拷贝
sheep.friend = (Sheep) this.friend.clone();
}
if(this.master !=null){ //实现对Person引用类型的的深拷贝
sheep.master = (Person) this.master.clone();
}
return sheep;
}
//深拷贝 - 方式2 通过对象的序列化实现 (推荐)
public Object deepClone() {
Sheep sheep=null;
//创建流对象
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = 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 (IOException e) {
e.printStackTrace();
}
}
return sheep;
}
}
class Person implements Serializable,Cloneable{
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
protected Object clone() {
Person p =null;
try {
p= (Person)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return p;
}
}
深拷贝实现方式1:重写clone方法来实现深拷贝
深拷贝实现方式2:通过对象序列化实现深拷贝(推荐)