请选择 进入手机版 | 继续访问电脑版

技术控

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

[其他] A Look at the Prototype Design Pattern

[复制链接]
慌乱了年华 发表于 2016-10-4 13:39:54
92 0
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!
我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表