<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    人在江湖

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

    單純fly weight

       1: //抽象享元角色
       2: abstract public class Flyweight
       3: {
       4:     abstract public void operation(String state);
       5: }
       6:  

     

       1: //具體享元(ConcreteFlyweight)角色
       2: public class ConcreteFlyweight extends Flyweight
       3: {
       4:     private Character intrinsicState = null;
       5:  
       6:  public ConcreteFlyweight(Character state)
       7:  {
       8:   this.intrinsicState = state;
       9:  }
      10:  
      11:  public void operation(String state)
      12:  {
      13:   System.out.print( "\nIntrinsic State = " + intrinsicState +
      14:             ", Extrinsic State = " + state);
      15:  }
      16: }
      17:  

     

       1: //享元工廠(FlyweightFactory)角色
       2: import java.util.Map;
       3: import java.util.HashMap;
       4: import java.util.Iterator;
       5:  
       6: public class FlyweightFactory
       7: {
       8:     private HashMap flies = new HashMap();
       9:     /**
      10:      * @link aggregation
      11:      * @directed
      12:      * @clientRole Flyweights
      13:      */
      14:     private Flyweight lnkFlyweight;
      15:  
      16:  public FlyweightFactory(){}
      17:  
      18:  public synchronized Flyweight factory(Character state)
      19:  {
      20:   if ( flies.containsKey( state ) )
      21:         {
      22:             return (Flyweight) flies.get( state );
      23:         }
      24:         else
      25:         {
      26:    Flyweight fly = new ConcreteFlyweight( state );
      27:             flies.put( state , fly);
      28:             return fly;
      29:         }
      30:  }
      31:  
      32:  public void checkFlyweight()
      33:  {
      34:   Flyweight fly ;
      35:         int i = 0;
      36:  
      37:         System.out.println("\n==========checkFlyweight()=============");
      38:   for ( Iterator it = flies.entrySet().iterator() ; it.hasNext() ;  )
      39:         {
      40:    Map.Entry e = (Map.Entry) it.next();
      41:             System.out.println("Item " + (++i) + " : " + e.getKey());
      42:         }
      43:         System.out.println("==========checkFlyweight()=============");
      44:  }
      45:  
      46: }
      47:  

     

       1:  
       2: //客戶端(Client)角色
       3: public class Client
       4: {
       5:     private static FlyweightFactory factory;
       6:  
       7:     static public void main(String[] args)
       8:  {
       9:   factory = new FlyweightFactory();
      10:  
      11:   Flyweight fly = factory.factory(new Character('a'));
      12:   fly.operation("First Call");
      13:   
      14:   fly = factory.factory(new Character('b'));
      15:   fly.operation("Second Call");
      16:   
      17:   fly = factory.factory(new Character('a'));
      18:   fly.operation("Third Call");
      19:   
      20:   // intrinsic Flyweight
      21:   factory.checkFlyweight();
      22:  }
      23: }
      24:  

    復(fù)合fly weight

       1: //抽象享元角色
       2: abstract public class Flyweight
       3: {
       4:     abstract public void operation(String state);
       5: }

       1: //具體享元(ConcreteFlyweight)角色
       2: public class ConcreteFlyweight extends Flyweight
       3: {
       4:     private Character intrinsicState = null;
       5:  
       6:  public ConcreteFlyweight(Character state)
       7:  {
       8:   this.intrinsicState = state;
       9:  }
      10:  
      11:  public void operation(String state)
      12:  {
      13:   System.out.print( "\nInternal State = " +
      14:             intrinsicState + " Extrinsic State = " + state );
      15:  }
      16: }
      17:  

       1: //復(fù)合享元(UnsharableFlyweight)角色
       2: import java.util.Map;
       3: import java.util.HashMap;
       4: import java.util.Iterator;
       5:  
       6: public class ConcreteCompositeFlyweight extends Flyweight
       7: {
       8:     private HashMap flies = new HashMap(10);
       9:  
      10:     /**
      11:      * @link aggregation
      12:      * @directed
      13:      * @clientRole Composite
      14:      */
      15:     private Flyweight flyweight;
      16:  
      17:  public ConcreteCompositeFlyweight()
      18:  {
      19:  }
      20:  
      21:  public void add(Character key, Flyweight fly)
      22:  {
      23:   flies.put(key, fly);
      24:  }
      25:  
      26:  public void operation(String extrinsicState)
      27:  {
      28:         Flyweight fly =  null;
      29:  
      30:   for ( Iterator it = flies.entrySet().iterator() ; it.hasNext() ;  )
      31:         {
      32:    Map.Entry e = (Map.Entry) it.next();
      33:             fly = (Flyweight) e.getValue();
      34:  
      35:             fly.operation(extrinsicState);
      36:         }
      37:  }
      38: }
      39:  
       1: //享元工廠(FlyweightFactory)角色
       2: import java.util.Map;
       3: import java.util.HashMap;
       4: import java.util.Iterator;
       5:  
       6: public class FlyweightFactory
       7: {
       8:     private HashMap flies = new HashMap();
       9:     /**
      10:      * @link aggregation
      11:      * @directed
      12:      * @clientRole Flyweights
      13:      */
      14:     private Flyweight lnkFlyweight;
      15:  
      16:  public FlyweightFactory(){}
      17:  
      18:  public Flyweight factory(String compositeState)
      19:  {
      20:   ConcreteCompositeFlyweight compositeFly = new ConcreteCompositeFlyweight();
      21:   
      22:   int length = compositeState.length();
      23:         Character state = null;
      24:  
      25:   for(int i = 0; i < length; i++)
      26:   {
      27:    state = new Character(compositeState.charAt(i) );
      28:             System.out.println("factory(" + state + ")");
      29:    compositeFly.add( state, this.factory( state) );
      30:   }
      31:   return compositeFly;
      32:     }
      33:  
      34:  public Flyweight factory(Character state)
      35:  {
      36:   if ( flies.containsKey( state ) )
      37:         {
      38:             return (Flyweight) flies.get( state );
      39:         }
      40:         else
      41:         {
      42:    Flyweight fly = new ConcreteFlyweight( state );
      43:             flies.put( state , fly);
      44:             return fly;
      45:         }
      46:  }
      47:  
      48:  public void checkFlyweight()
      49:  {
      50:   Flyweight fly ;
      51:         int i = 0 ;
      52:  
      53:         System.out.println("\n==========checkFlyweight()=============");
      54:   for ( Iterator it = flies.entrySet().iterator() ; it.hasNext() ;  )
      55:         {
      56:    Map.Entry e = (Map.Entry) it.next();
      57:             System.out.println( "Item " + (++i) + " : " + e.getKey());
      58:         }
      59:         System.out.println("\n==========checkFlyweight()=============");
      60:  }
      61:  
      62: }
      63:  

       1: //客戶端(Client)角色
       2: public class Client
       3: {
       4:     private static FlyweightFactory factory;
       5:  
       6:     public static void main(String[] args)
       7:  {
       8:   factory = new FlyweightFactory();
       9:  
      10:   Flyweight fly;
      11: /*        fly = factory.factory(new Character('a'));
      12:   fly.operation();
      13:   
      14:   fly = factory.factory(new Character('b'));
      15:   fly.operation();
      16:   
      17:   fly = factory.factory(new Character('a'));
      18:   fly.operation();
      19: */
      20:         fly = factory.factory("abc");
      21:         fly.operation("Composite Call");
      22:   
      23: // intrinsic Flyweight
      24:   factory.checkFlyweight();
      25:  }
      26: }
      27:  
      28:  
      29:  
      30:  
    posted on 2011-02-12 23:57 人在江湖 閱讀(665) 評(píng)論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 亚洲电影免费在线观看| 麻豆国产人免费人成免费视频| 亚洲欧洲日产国码二区首页| 久久久www成人免费毛片| 色吊丝性永久免费看码| 久久久久亚洲AV成人片| 免费h黄肉动漫在线观看| 午夜精品免费在线观看| 久久精品国产亚洲AV电影网| 亚洲香蕉网久久综合影视| 啦啦啦高清视频在线观看免费| jizz在线免费观看| 456亚洲人成在线播放网站| 国产亚洲情侣一区二区无| 4虎永免费最新永久免费地址| 污污污视频在线免费观看| 亚洲a级片在线观看| 久久伊人亚洲AV无码网站| 国产精品美女午夜爽爽爽免费| 久久久受www免费人成| 亚洲中文字幕久久久一区| 亚洲Aⅴ无码专区在线观看q| jizzjizz亚洲| 成人午夜性A级毛片免费| 午夜视频在线免费观看| jizz免费观看视频| 亚洲人成色4444在线观看| 久久精品国产亚洲av影院| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 亚洲av永久无码精品秋霞电影影院 | 久热综合在线亚洲精品| yy6080久久亚洲精品| 免费看黄视频网站| 一级毛片不卡片免费观看| 精品乱子伦一区二区三区高清免费播放 | 亚洲91精品麻豆国产系列在线| 色噜噜AV亚洲色一区二区| 免费a在线观看播放| 好先生在线观看免费播放 | 最近免费中文字幕视频高清在线看| 日韩视频免费在线观看|