設計模式-23種設計模式-建立型-原型模式
阿新 • • 發佈:2021-07-01
一、原型模式介紹
二、原型模式引入
需求:
傳統方式UML類圖:
程式碼實現(Java):
public class Sheep { 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 + '\'' + '}'; } }
public class Client { public static void main(String[] args) {//傳統方法 Sheep sheep = new Sheep("tom", 1, "白色"); Sheep sheep1 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); 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()); System.out.println(sheep); System.out.println(sheep1); System.out.println(sheep2); System.out.println(sheep3); System.out.println(sheep4); System.out.println(sheep5); } }
原型模式UML類圖:
原型模式(淺拷貝Java實現):
public class Sheep implements Cloneable{ private String name; private int age; private String color; private String address = "蒙古羊"; public Sheep friend; //是物件,克隆會如何處理 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 + '\'' + ", address='" + address + '\'' + '}'; } //克隆該例項,使用預設的clone方法來完成 @Override protected Object clone() { Sheep sheep = null; try { sheep = (Sheep)super.clone(); } catch (Exception e) { System.out.println(e.getMessage()); } return sheep; } }
public class Client { public static void main(String[] args) { System.out.println("原型模式完成物件的建立"); Sheep sheep = new Sheep("tom", 1, "白色"); sheep.friend = new Sheep("jack", 2, "黑色"); Sheep sheep1 = (Sheep) sheep.clone(); Sheep sheep2 = (Sheep) sheep.clone(); Sheep sheep3 = (Sheep) sheep.clone(); Sheep sheep4 = (Sheep) sheep.clone(); System.out.println("sheep1 = "+sheep1 + "sheep1.friend="+sheep1.friend.hashCode()); System.out.println("sheep2 = "+sheep2 + "sheep2.friend="+sheep2.friend.hashCode()); System.out.println("sheep3 = "+sheep3 + "sheep3.friend="+sheep3.friend.hashCode()); System.out.println("sheep4 = "+sheep4 + "sheep4.friend="+sheep4.friend.hashCode()); } }
原型模式(深拷貝Java實現):
import java.io.Serializable; public class DeepCloneableTarget implements Serializable, Cloneable { /** * */ private static final long serialVersionUID = 1L; private String cloneName; private String cloneClass; //構造器 public DeepCloneableTarget(String cloneName, String cloneClass) { this.cloneName = cloneName; this.cloneClass = cloneClass; } //因為該類的屬性,都是String , 因此我們這裡使用預設的clone完成即可 @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
import java.io.*; public class DeepProtoType implements Serializable, Cloneable{ public String name; //String 屬性 public DeepCloneableTarget deepCloneableTarget;// 引用型別 public DeepProtoType() { super(); } //深拷貝 - 方式 1 使用clone 方法 @Override protected Object clone() throws CloneNotSupportedException { Object deep = null; //這裡完成對基本資料型別(屬性)和String的克隆 deep = super.clone(); //對引用型別的屬性,進行單獨處理 DeepProtoType deepProtoType = (DeepProtoType)deep; deepProtoType.deepCloneableTarget = (DeepCloneableTarget)deepCloneableTarget.clone(); return deepProtoType; } //深拷貝 - 方式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 copyObj = (DeepProtoType)ois.readObject(); return copyObj; } catch (Exception e) { e.printStackTrace(); return null; } finally { //關閉流 try { bos.close(); oos.close(); bis.close(); ois.close(); } catch (Exception e2) { System.out.println(e2.getMessage()); } } } }
public class Client { public static void main(String[] args) throws Exception { DeepProtoType p = new DeepProtoType(); p.name = "宋江"; p.deepCloneableTarget = new DeepCloneableTarget("大牛", "小牛"); //方式1 完成深拷貝 // DeepProtoType p2 = (DeepProtoType) p.clone(); // // System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode()); // System.out.println("p2.name=" + p.name + "p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode()); //方式2 完成深拷貝 DeepProtoType p2 = (DeepProtoType) p.deepClone(); System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode()); System.out.println("p2.name=" + p.name + "p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode()); } }