1、前言
当需要克隆100个名称为Dolly 年龄 1的羊,以往的写法都是直接new 100个Sheep对象;
public class PrototypeDemo {
public static void main(String[] args) {
Sheep dolly1 = new Sheep("Dolly", 1);
Sheep dolly2 = new Sheep("Dolly", 1);
Sheep dolly3 = new Sheep("Dolly", 1);
Sheep dolly4 = new Sheep("Dolly", 1);
Sheep dolly100 = new Sheep("Dolly", 1);
}
@Data
static
class Sheep {
private String name;
private int age;
public Sheep (String name,int age){
this.name = name;
this.age = age;
}
}
}
- 优点
- 缺点
- 在创建新的对象时,总是需要重新获取原始对象的属性,如果创建的对象比较复杂时,效率较低
- 总是需要重新初始化对象,而不是动态地获得对象运行时的状态,不够灵活
2、什么是原型模式
原型模式(Prototype Pattern)通过拷贝这些原型,创建新的对象,是用于创建重复的对象,同时又能保证性能。原型模式属于创建型模式,它提供了一种创建对象的最佳方式。
原型模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。
Java中Object类提供了一个clone()方法,该方法可以将一个Java对象复制一份,但是需要实现clone的Java类必须要实现一个接口Cloneable,该接口表示该类能够复制且具有复制的能力
3、原型模式实现
public class PrototypeDemo {
public static void main(String[] args) throws Exception {
prototype();
}
private static void prototype() throws Exception {
ArrayList arrayList = new ArrayList();
arrayList.add("foreFoot");
Sheep dolly1 = new Sheep("Dolly", 1,arrayList);
Sheep dolly2 = (Sheep)dolly1.clone();
ArrayList list = dolly2.getListFoot();
list.add("hindFoot2");
Sheep dolly3 = (Sheep)dolly1.deepClone();
ArrayList list3 = dolly3.getListFoot();
list3.add("hindFoot3");
System.out.println(dolly1 == dolly2);
System.out.println("dolly1.list="+dolly1.getListFoot());
System.out.println("dolly2.list="+dolly2.getListFoot());
System.out.println("dolly3.list="+dolly3.getListFoot());
}
private static void tradition() {
Sheep dolly1 = new Sheep("Dolly", 1,null);
Sheep dolly2 = new Sheep("Dolly", 1,null);
Sheep dolly3 = new Sheep("Dolly", 1,null);
Sheep dolly4 = new Sheep("Dolly", 1,null);
Sheep dolly100 = new Sheep("Dolly", 1,null);
}
}
public class Sheep implements Cloneable, Serializable {
private String name;
private int age;
private ArrayList listFoot;
public Sheep (){
}
public Sheep (String name,int age,ArrayList listFoot){
this.name = name;
this.age = age;
this.listFoot = listFoot;
}
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 ArrayList getListFoot() {
return listFoot;
}
public void setListFoot(ArrayList listFoot) {
this.listFoot = listFoot;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Sheep sheep = (Sheep)super.clone();
sheep.listFoot = (ArrayList) this.listFoot.clone();
return sheep;
}
protected Object deepClone() throws Exception{
ByteArrayOutputStream baos =new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return (Sheep) ois.readObject();
}
}
4、原型模式优缺点
5、原型模式的更多实现
|