享元模式

壹頁書發表於2017-03-29


http://www.cnblogs.com/java-my-life/archive/2012/04/26/2468499.html




示例:
  1. import java.util.HashMap;  
  2. import java.util.Iterator;  
  3. import java.util.Map;  
  4.   
  5. public class Test {  
  6.     /** 
  7.      * 複合的享元模式: 複合享元是不能共享的。 但是複合享元是可以分解為可共享的單純享元。 
  8.      */  
  9.     public static void main(String[] args) {  
  10.         FlyweightFactory f = new FlyweightFactory();  
  11.   
  12.         Flyweight fly = f.factory("aba");  
  13.         fly.operation("charles");  
  14.   
  15.         f.checkFlyweight();  
  16.   
  17.     }  
  18. }  
  19.   
  20. /** 
  21.  * 抽象享元 
  22.  */  
  23. abstract class Flyweight {  
  24.     public abstract void operation(String extrinsicState);  
  25. }  
  26.   
  27. /** 
  28.  * 具體享元 
  29.  */  
  30. class ConcreteFlyweight extends Flyweight {  
  31.   
  32.     private Character inState;  
  33.   
  34.     public ConcreteFlyweight(Character inState) {  
  35.         this.inState = inState;  
  36.     }  
  37.   
  38.     /** 
  39.      * 外蘊狀態改變方法行為,但不會改變內蘊狀態 
  40.      */  
  41.     @Override  
  42.     public void operation(String extState) {  
  43.         String str = "inState:" + inState + ";extState:" + extState;  
  44.         System.out.println(str);  
  45.     }  
  46.   
  47. }  
  48.   
  49. /** 
  50.  * 複合享元 
  51.  */  
  52. class CompositeFlyweight extends Flyweight {  
  53.     private Map<Character, Flyweight> map;  
  54.   
  55.     public CompositeFlyweight() {  
  56.         map = new HashMap<Character, Flyweight>();  
  57.     }  
  58.   
  59.     public void add(Character c, Flyweight fly) {  
  60.         map.put(c, fly);  
  61.     }  
  62.   
  63.     @Override  
  64.     public void operation(String extState) {  
  65.         Iterator<Map.Entry<Character, Flyweight>> it = map.entrySet().iterator();  
  66.         while (it.hasNext()) {  
  67.             Map.Entry<Character, Flyweight> entry = it.next();  
  68.             Flyweight fly = entry.getValue();  
  69.             fly.operation(extState);  
  70.         }  
  71.     }  
  72. }  
  73.   
  74. /** 
  75.  * 享元工廠 
  76.  */  
  77. class FlyweightFactory {  
  78.     private Map<Character, Flyweight> map;  
  79.   
  80.     public FlyweightFactory() {  
  81.         map = new HashMap<Character, Flyweight>();  
  82.     }  
  83.   
  84.     /** 
  85.      * 單純享元工廠 
  86.      */  
  87.     public Flyweight factory(Character state) {  
  88.   
  89.         Flyweight flyweight;  
  90.   
  91.         if (map.containsKey(state)) {  
  92.             flyweight = map.get(state);  
  93.         } else {  
  94.             flyweight = new ConcreteFlyweight(state);  
  95.             map.put(state, flyweight);  
  96.         }  
  97.   
  98.         return flyweight;  
  99.     }  
  100.   
  101.     /** 
  102.      * 複合享元工廠 此處Character的複合型別恰好是String, 當無此巧合時,可使用List等聚集型別傳入. 
  103.      */  
  104.     public Flyweight factory(String compositeState) {  
  105.         CompositeFlyweight composite = new CompositeFlyweight();  
  106.   
  107.         for (int i = 0; i < compositeState.length(); i++) {  
  108.             Character c = new Character(compositeState.charAt(i));  
  109.             composite.add(c, this.factory(c));  
  110.         }  
  111.   
  112.         return composite;  
  113.     }  
  114.   
  115.     /** 
  116.      * 輔助方法 
  117.      */  
  118.     public void checkFlyweight() {  
  119.         Iterator<Map.Entry<Character, Flyweight>> it = map.entrySet().iterator();  
  120.         System.out.println("checkFlyweight:");  
  121.         while (it.hasNext()) {  
  122.             Map.Entry<Character, Flyweight> entry = it.next();  
  123.             Character key = entry.getKey();  
  124.             System.out.println("key:" + key);  
  125.         }  
  126.     }  
  127.   






來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2136280/,如需轉載,請註明出處,否則將追究法律責任。

相關文章