java學習

          java學習

           

          設計模式之策略模式

          定義一系列的算法,把它們一個個封裝起來,并且使它們可相互替換。本模式使得算法可獨立于使用它的客戶而變化。
          適合場合:
           * 1.以不同的格式保存文件
           * 2.以不同的算法壓縮文件
           * 3.以不同的算法截取圖形
           * 4.以不同的格式輸出數(shù)據(jù)的圖形,曲線,框圖等
          策略接口:
          public interface Strategy {
              void method();

          }
          策略接口實現(xiàn)類:
          public class StrategyImplA implements Strategy{
              public void method() {
                  System.out.println("這是第一個實現(xiàn)a");
              }

          }
          public class StrategyImplB implements Strategy{
              public void method() {
                  System.out.println("這是第二個實現(xiàn)b");
              }

          }

          public class StrategyImplC implements Strategy{
               public void method() {
                      System.out.println("這是第三個實現(xiàn)c");
                  }

          }
          調用類:
          public class Context {
          Strategy stra;
              
              public Context(Strategy stra) {
                  this.stra = stra;
              }
              
              public void doMethod() {
                  stra.method();
              }

          }
          測試:
          public class Test {
              public static void main(String[] args) {
                  Context ctx = new Context(new StrategyImplA());
                  ctx.doMethod();
                  
                  ctx = new Context(new StrategyImplB());
                  ctx.doMethod();
                  
                  ctx = new Context(new StrategyImplC());
                  ctx.doMethod();
              }

          }

          posted on 2013-04-23 13:09 楊軍威 閱讀(136) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          導航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 定西市| 温州市| 老河口市| 白山市| 左权县| 永清县| 伊川县| 宁晋县| 桃源县| 临沧市| 唐山市| 余干县| 昌乐县| 崇左市| 华阴市| 和政县| 哈巴河县| 五台县| 侯马市| 高尔夫| 石泉县| 海丰县| 潍坊市| 新泰市| 庐江县| 辛集市| 灌阳县| 灌云县| 绥德县| 惠安县| 华池县| 石狮市| 双流县| 普洱| 崇仁县| 岳池县| 隆子县| 酉阳| 敦化市| 巩义市| 化州市|