技术控

    今日:41| 主题:49409
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] A Look at the Prototype Design Pattern

[复制链接]
慌乱了年华 发表于 2016-10-4 13:39:54
62 0

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
This is a part of the Creational pattern, which helps to create objects as requested. This pattern uses the philosophy of cloning to create objects, and we know that two methods of cloning are possible. One is shallow cloning, and the other is deep cloning. The behavior of both cloning types is different, so choose wisely.
  You can learn    more about cloning here.  
  When to Use the Prototype Pattern  

  
       
  •       If the creation of objects is complex or costly.
       
  •       If the addition or removal of objects is expected on runtime.
       
  •       If the client should be unaware of the object creation.
       
  •       If an object similar to the existing one is required.
      
  Scenario

  When creating a bank application, we know that bank transactions need expensive database queries. Transactions would be linked with bank accounts, which may be an individual or group and with their profiles with the bank. So, in this case, once the actual object is created (exactly one single object), it may be required further in a number of places. The Prototype pattern can be used to get the copy of that object, which can replace the old object.  
  PrototypeClient.java  

  1. public class PrototypeClient {
  2.     public static void main( String[] args ) {
  3.         Shoes xyz = Factory.makeObject("Sport");
  4.         xyz.wear();
  5.     }
  6. }
复制代码
Shoes.java  

  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.Serializable;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. public abstract class Shoes implements Serializable, Cloneable{  
  10.     private static final long serialVersionUID = -1937047519230746677L;
  11.     abstract void wear();
  12.     public Object shallowCloning() throws CloneNotSupportedException    {
  13.         return this.clone();
  14.     }
  15.     public Shoes deepCloning() throws IOException, ClassNotFoundException {
  16.         //Serialization of object
  17.         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  18.         ObjectOutputStream out = new ObjectOutputStream(bos);
  19.         out.writeObject(this);
  20.         //De-serialization of object
  21.         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
  22.         ObjectInputStream in = new ObjectInputStream(bis);
  23.         Shoes clonedShoes = (Shoes) in.readObject();
  24.         return clonedShoes;
  25.     }
  26. }
复制代码
SportShoes.java  

  1. public class SportShoes extends Shoes {
  2.     private static final long serialVersionUID = 440347043243434494L;
  3.     public String toString() {
  4.         return "SportShoes";
  5.     }
  6.     @Override
  7.     public void wear() {
  8.         //some SportShoes specific code to wear
  9.         System.out.println("I am wearing " + toString());
  10.     }
  11. }
复制代码
CasualShoes.java  

  1. public class CasualShoes extends Shoes {
  2.     private static final long serialVersionUID = -6493171299609719559L;
  3.     public String toString() {
  4.         return "CasualShoes";
  5.     }
  6.     @Override
  7.     public void wear() {
  8.         //some CasualShoes specific code to wear
  9.         System.out.println("I am wearing " + toString());
  10.     }
  11. }
复制代码
FormalShoes.java  

  1. public class FormalShoes extends Shoes {
  2.     private static final long serialVersionUID = -5670125744755511170L;
  3.     public String toString() {
  4.         return "FormalShoes";
  5.     }
  6.     @Override
  7.     public void wear() {
  8.         //some FormalShoes specific code to wear
  9.         System.out.println("I am wearing " + toString());
  10.     }
  11. }
复制代码
Factory.java  

  1. public class Factory {
  2.     private static Map<String, Object> prototypes = new HashMap<String, Object>();
  3.     static {
  4.         prototypes.put( "Sport",   new SportShoes() );
  5.         prototypes.put( "Casual",  new CasualShoes() );
  6.         prototypes.put( "Formal", new FormalShoes() );
  7.     }
  8.     public static Shoes makeObject(String shoesType) {
  9.         Shoes shoes = (Shoes)prototypes.get(shoesType);
  10.         //deep cloning (using in-memory)
  11.         try {
  12.             Shoes deepClonedShoes = shoes.deepCloning();
  13.             System.out.println("Deep-Cloned Shoes ========== "+deepClonedShoes);
  14.         } catch (IOException e) {
  15.             e.printStackTrace();
  16.         } catch (ClassNotFoundException e) {
  17.             e.printStackTrace();
  18.         }
  19.         //Shallow cloning
  20.         Shoes shallowClonedShoes = null;
  21.         try {
  22.             shallowClonedShoes = (Shoes) shoes.shallowCloning();
  23.         } catch (CloneNotSupportedException e) {
  24.             e.printStackTrace();
  25.         }
  26.         System.out.println("Shallow-Cloned Shoes ========== " +shallowClonedShoes );
  27.         return shallowClonedShoes;
  28.     }
  29. }
复制代码
Output:  

  1. Deep-Cloned Shoes ========== SportShoes
  2. Shallow-Cloned Shoes ========== SportShoes
  3. I am wearing SportShoes
复制代码
I hope this creates a clear understanding about the Prototype design pattern and how to implement it. Happy Learning!
友荐云推荐




上一篇:What’s new in PostgreSQL 9.5
下一篇:Editorial: How Do You Keep Your Skill Set Relevant?
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表