设计模式-原型模式
原型模式分为:不通过构造函数的方式创建对象,也就是copy对象。
浅克隆和深克隆两种
原型模式的有点:
性能优良,Java自带的原型模式,是基于内存二进制流的拷贝,比直接new一个对象性能上提升了许多;
可以使用深克隆方式保存对象的状态,使用原型模式将对象复制一份并将其状态保存起来,简化了创建过程。
原型模式的缺点:
必须配备克隆方法;
当对已有类进行改造的时候,需要修改代码,违反了开闭原则。
一、通常写法:
克隆的接口
public interface IPrototype<T> {
T clone();
}
实现克隆的接口,然后硬编码,重写clone方法
public class ConcretePrototype implements IPrototype {
private String name;
private int 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 "ConcretePrototype{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public ConcretePrototype clone() {
ConcretePrototype prototype = new ConcretePrototype();
prototype.setAge(this.getAge());
prototype.setName(this.getName());
return prototype;
}
}
测试类
public class Client {
public static void main(String[] args) {
ConcretePrototype prototype = new ConcretePrototype();
prototype.setName("张三");
prototype.setAge(18);
System.out.println("原型对象: " + prototype);
ConcretePrototype clone = prototype.clone();
System.out.println("克隆对象: " + clone);
}
}
运行结果
原型对象: ConcretePrototype{name='张三', age=18}
克隆对象: ConcretePrototype{name='张三', age=18}
二、实现Cloneable接口的浅克隆
只要实现Cloneable接口的都是浅克隆
public class ConcretePrototype implements Cloneable {
private String name;
private int age;
private List<String> hobbs;
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 List<String> getHobbs() {
return hobbs;
}
public void setHobbs(List<String> hobbs) {
this.hobbs = hobbs;
}
@Override
public String toString() {
return "ConcretePrototype{" +
"name='" + name + '\'' +
", age=" + age +
", hobbs=" + hobbs +
'}';
}
@Override
public ConcretePrototype clone() {
try {
return (ConcretePrototype) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
}
三、深克隆
public class ConcretePrototype implements Cloneable,Serializable{
private String name;
private int age;
private List<String> hobbs;
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 List<String> getHobbs() {
return hobbs;
}
public void setHobbs(List<String> hobbs) {
this.hobbs = hobbs;
}
@Override
public String toString() {
return "ConcretePrototype{" +
"name='" + name + '\'' +
", age=" + age +
", hobbs=" + hobbs +
'}';
}
// 浅克隆
@Override
public ConcretePrototype clone() {
try {
return (ConcretePrototype) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
// 深克隆
public ConcretePrototype deepClone() {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return (ConcretePrototype) ois.readObject();
} catch (Exception e) {
e.getMessage();
}
return null;
}
// 深克隆2
public ConcretePrototype deepJsonClone() {
JSONObject obj = (JSONObject) JSONObject.toJSON(this);
ConcretePrototype prototype = JSONObject.toJavaObject(obj, ConcretePrototype.class);
return prototype;
}
}