目录
克隆羊问题
传统方式
原型模式
原型模式在Spring框架中源码分析
原型模式-深浅拷贝
浅拷贝
深拷贝
代码案例
方式一:重写clone方法来实现
方式二:通过对象序列化实现
原型模式的注意事项和细节
现在有一只羊,姓名为:tom,年龄为:1,颜色为:白色,请编写程序创建和tom羊,属性完全相同的10只羊
package prototype; public class Client { public static void main(String[] args) { //传统的方法 Sheep sheep = new Sheep("tom", 1, "白色"); Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); Sheep sheep4 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); Sheep sheep5 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); Sheep sheep6 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); Sheep sheep7 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); } }优缺点
优点是比较好理解,简单易操作在创建新的对象时,总是需要获取原始对象的属性,如果创建的对象比较复杂时,效率较低总是需要重新初始化对象,而不是动态地获得对象运行时的状态,不够灵活改进的思路分析
Java中Object类是所有类的根类, Object类提供了一个clone方法,该方法可以将一个Java对象复制一份,但是需要实现一个接口Clonable,该接口表示该类能够复制且具有复制的能力-》原型模式
Sheep类
package prototype.improve; public 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; } @Override public String toString() { return "Sheep{" + "name='" + name + '\'' + ", age=" + age + ", color='" + color + '\'' + '}'; } //克隆该实例,使用默认的克隆方法来完成 @Override protected Object clone() { Sheep sheep = null; try { sheep = (Sheep) super.clone(); }catch (Exception e){ System.out.println(e.getMessage()); } return sheep; } }Client类使用形式
运行结果
输出的hashcode值不相同
写起来麻烦,但是用起来方便(推荐使用)
//深拷贝 - 方式2 通过对象的序列化实现(推荐使用) public Object deepClone(){ //创建流对象 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); DeepProtoType copyObject = (DeepProtoType)ois.readObject(); return copyObject; }catch (Exception e){ System.out.println(e.getMessage()); return null; }finally { //关闭流 try{ bos.close(); oos.close(); bis.close(); ois.close(); }catch (Exception e2){ System.out.println(e2.getMessage()); } } } package prototype.deepclone; public class Client { public static void main(String[] args) { DeepProtoType p = new DeepProtoType(); p.name = "宋江"; DeepCloneableTarget deepCloneableTarget = new DeepCloneableTarget("大牛","小牛"); p.deepCloneableTarget = deepCloneableTarget; //方式1完成深拷贝 DeepProtoType p2 = (DeepProtoType) p.deepClone(); System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode()); System.out.println("p2.name=" + p2.name + "p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode()); } }