<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
用原型範例指定建立物件的種類,並且通過拷貝這些原型建立新的物件。
原型模式其實就是從一個物件在建立另外一個可客製化的物件,不需要知道任何建立的細節
在執行期建立和刪除原型。
經常用於:
類初始化消耗資源較多
建構函式比較複雜
1.實現cloneable 介面,重寫Object的clone方法
2.利用已有的一個原型物件,快速地生成和原型物件一樣的範例。
淺複製:基本資料型別進行值傳遞、參照資料型別的指標仍是指向原來的物件(成員變數)。
深複製:基本資料型別進行值傳遞、參照資料型別開闢新的記憶體空間。
需求:實現克隆羊
有一頭羊,需要經過賦值再克隆出兩頭。
/** * 克隆羊類 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/9/3 - 14:51 */ public class Sheep { private String name; private Integer age; private String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Sheep() { } public Sheep(String name, Integer age, String color) { this.name = name; this.age = age; this.color = color; } }
Main方法
/** * * 傳統模式 * @author Promsing(張有博) * @version 1.0.0 * @since 2022/9/3 - 14:47 */ public class Client { public static void main(String[] args) { //小羊 Sheep sheep=new Sheep("小紅",8,"紅色"); //克隆羊 Sheep sheep2=sheep; Sheep sheep3=sheep; System.out.println("通過賦值,指標還是指向sheep"); System.out.println(sheep); System.out.println(sheep2); System.out.println(sheep3); } } //通過賦值,指標還是指向sheep //com.promsing.creational.prototype.type1.Sheep@1b6d3586 //com.promsing.creational.prototype.type1.Sheep@1b6d3586 //com.promsing.creational.prototype.type1.Sheep@1b6d3586
Cloneable是標記型的介面,它們內部都沒有方法和屬性,實現 Cloneable來表示該物件能被克隆,能使用Object.clone()方法。
如果沒有實現 Cloneable的類物件呼叫clone()就會丟擲CloneNotSupportedException。
實現Cloneable預設是淺複製
/** * 克隆羊 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/9/3 - 14:53 */ public class Sheep implements Cloneable { private String name; private Integer age; private String color; /** * 羊的朋友 */ private Sheep friend; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Sheep() { } public Sheep getFriend() { return friend; } public void setFriend(Sheep friend) { this.friend = friend; } public Sheep(String name, Integer age, String color) { this.name = name; this.age = age; this.color = color; } /** * 克隆該範例,呼叫object.clone的方法 * @return */ @Override protected Sheep clone() { Sheep sheep = null; try { sheep = (Sheep) super.clone(); } catch (CloneNotSupportedException e) { System.out.println(e.getMessage()); e.printStackTrace(); } return sheep; } }
Main方法
/** * 淺複製:淺複製:基本資料型別進行值傳遞、參照資料型別的指標仍是指向原來的物件(成員變數)。 */ public class ShallowClient { public static void main(String[] args) { Sheep sheep=new Sheep("小紅",9,"紅色"); sheep.setFriend(new Sheep("小黑",10,"黑色")); Sheep sheep1 = sheep.clone();//開闢了新的空間 Sheep sheep2 = sheep.clone(); //淺複製 System.out.println("原型羊"+sheep); System.out.println("克隆羊"+sheep1+"-朋友羊-"+sheep1.getFriend()); System.out.println("克隆羊"+sheep2+"-朋友羊-"+sheep2.getFriend()); //原型羊com.promsing.creational.prototype.type3.Sheep@1b6d3586 //克隆羊com.promsing.creational.prototype.type3.Sheep@4554617c-朋友羊-com.promsing.creational.prototype.type3.Sheep@74a14482 //克隆羊com.promsing.creational.prototype.type3.Sheep@1540e19d-朋友羊-com.promsing.creational.prototype.type3.Sheep@74a14482 } }
一定要實現介面cloneable 否則報錯:
Exception in thread "main" java.lang.CloneNotSupportedException: com.promsing.creational.prototype.type4.Sheep
at java.lang.Object.clone(Native Method)
at com.promsing.creational.prototype.type4.Sheep.clone(Sheep.java:76)
at com.promsing.creational.prototype.type4.DeepClient.main(DeepClient.java:14)
羊類、房子類都需要實現Cloneable介面
房子類
/** * 房子類 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/9/3 - 15:27 */ public class House implements Cloneable { //地址 private String address; //尺寸 private Integer size; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } /** * 克隆的方法 * @return * @throws CloneNotSupportedException */ @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
羊類
/** * 克隆羊-深拷貝 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/9/3 - 15:26 */ public class Sheep implements Cloneable { private String name; private Integer age; private String color; /** * 羊的房子:參照型別 */ private House house; public House getHouse() { return house; } public void setHouse(House house) { this.house = house; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Sheep() { } public Sheep(String name, Integer age, String color) { this.name = name; this.age = age; this.color = color; } /** * 克隆該範例,呼叫object,clone的方法 * * @return * @throws CloneNotSupportedException */ @Override protected Object clone() throws CloneNotSupportedException { //對基本資料型別進行處理 Sheep deep = null; deep = (Sheep) super.clone(); //對參照型別進行處理 //進行再次克隆 House clone = (House) deep.getHouse().clone(); deep.setHouse(clone); return deep; } }
Main
/** * 深複製:羊類、房子類都需要重寫clone的介面,比較麻煩。不符合開閉 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/9/3 - 15:38 */ public class DeepClient { public static void main(String[] args) throws CloneNotSupportedException { Sheep sheep=new Sheep("黑",90,"heisee"); sheep.setHouse(new House()); Sheep clone = (Sheep)sheep.clone(); System.out.println("原本的物件"); System.out.println(sheep); System.out.println(sheep.getHouse()); System.out.println("克隆的物件"); System.out.println(clone); System.out.println(clone.getHouse()); //開闢了新的記憶體空間 //原本的物件 //com.promsing.creational.prototype.type4.Sheep@1b6d3586 //com.promsing.creational.prototype.type4.House@4554617c //克隆的物件 //com.promsing.creational.prototype.type4.Sheep@74a14482 //com.promsing.creational.prototype.type4.House@1540e19d } }
羊類、房子類都需要實現Serializable介面。注意這裡可以不實現Cloneable了。
房子類
/** * 房子類 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/9/3 - 15:27 */ public class House implements Serializable{ //地址 private String address; //尺寸 private Integer size; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } }
羊類
/** * 克隆羊-深拷貝 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/9/3 - 15:26 */ public class Sheep implements Serializable { private String name; private Integer age; private String color; /** * 羊的房子:參照型別 */ private House house; public House getHouse() { return house; } public void setHouse(House house) { this.house = house; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Sheep() { } public Sheep(String name, Integer age, String color) { this.name = name; this.age = age; this.color = color; } /** * 序列化的方式:進行深複製 * 寫著麻煩:用著簡單。支援開閉原則 * @return */ 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); Sheep copyObj = (Sheep) ois.readObject(); return copyObj; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; } finally { //關閉流 try { bos.close(); oos.close(); bis.close(); ois.close(); } catch (Exception e2) { // TODO: handle exception System.out.println(e2.getMessage()); } } } }
Main
/** * 深複製 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/9/3 - 15:38 */ public class DeepClient { public static void main(String[] args) throws CloneNotSupportedException { Sheep sheep=new Sheep("黑",90,"heisee"); sheep.setHouse(new House()); Sheep clone = (Sheep)sheep.deepClone(); System.out.println("原本的物件"); System.out.println(sheep); System.out.println(sheep.getHouse()); System.out.println("克隆的物件"); System.out.println(clone); System.out.println(clone.getHouse()); } }
Spring使用原型模式:@scope(“prototype”)
public static void main(String[] args) throws IOException { //new一個容器 AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); System.out.println("run success"); OrderService bean = context.getBean(OrderService.class); } //根據getBean點進去 public <T> T getBean(Class<T> requiredType) throws BeansException { assertBeanFactoryActive(); return getBeanFactory().getBean(requiredType); } public Object getBean(String name) throws BeansException { return doGetBean(name, null, null, false); }
後續走到了。AbstractBeanFactory類中的doGetBean方法中裡邊程式碼做判斷mbd.isPrototype()
protected <T> T doGetBean( String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly) throws BeansException { String beanName = transformedBeanName(name); Object bean; // Eagerly check singleton cache for manually registered singletons. Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { //程式碼省略~~~~ } else { //程式碼省略~~~~ try { //程式碼省略~~~~ // Create bean instance. //判斷是否是單例 if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, () -> { try { return createBean(beanName, mbd, args); } catch (BeansException ex) { destroySingleton(beanName); throw ex; } }); bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } //判斷是否是多例(原型) //這裡會建立一個新的物件 else if (mbd.isPrototype()) { // It's a prototype -> create a new instance. Object prototypeInstance = null; try { beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } else { //程式碼省略~~~~ } } catch (BeansException ex) { cleanupAfterBeanCreationFailure(beanName); throw ex; } } //程式碼省略~~~~ } return (T) bean; }
ArrayList實現了Cloneable介面
public Object clone() { try { ArrayList<?> v = (ArrayList<?>) super.clone(); v.elementData = Arrays.copyOf(elementData, size); v.modCount = 0; return v; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(e); } }
到此這篇關於一文帶你瞭解Java設計模式之原型模式的文章就介紹到這了,更多相關Java原型模式內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45