活到老,學到老

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            9 Posts :: 1 Stories :: 2 Comments :: 0 Trackbacks

          2011年4月6日 #

               摘要:     概念:觀察者模式定義了一對多依賴,這樣一來,當一個對象改變狀態(tài)時,它的所有依賴者都會收到通知并自動更新。     舉個網(wǎng)上商城的例子,比如很多顧客對某個商品感興趣,把商品收藏,當該商品降價、促銷、有貨了等事件發(fā)生時,就會發(fā)Email通知顧客。     UML圖...  閱讀全文
          posted @ 2011-04-07 22:29 simon.shen 閱讀(332) | 評論 (1)編輯 收藏

              從今天開始,把常用的設計模式都簡單的整理一遍,希望每個星期能至少整理2個模式吧,先從簡單的策略模式開始。
              
              概念:它定義了一系列的算法,并將每一個算法封裝起來,而且使它們還可以相互替換。策略模式讓算法的變化不會影響到使用算法的客戶。
              
              策略模式很簡單,實際上就是OO中的多態(tài),舉個例子,某商場要進行促銷,對于普通顧客打88折,對于銀卡客戶在88折基礎上再滿400減160,對于金卡客戶在88折基礎上再滿400減200。
              
              
          UML圖如下所示:
              

              接口DiscountStrategy代碼如下:
              
          public interface DiscountStrategy {
              
          public double discount(double
           sum);
          }

              類GeneralDiscountStrategy類代碼如下:
              
          public class GeneralDiscountStrategy implements DiscountStrategy {

              @Override
              
          public double discount(double sum) 
          {
                  
          return sum * 0.88
          ;
              }


          }

              類SilverDiscountStrategy類代碼如下:
              
          public class SilverDiscountStrategy implements DiscountStrategy {

              @Override
              
          public double discount(double sum) 
          {
                  sum 
          = sum * 0.88
          ;
                  
          int t = (int) sum / 400
          ;
                  sum 
          = sum - t * 160
          ;
                  
          return
           sum;
              }

          }

              類GoldenDiscountStrategy代碼如下:
              
          public class GoldenDiscountStrategy implements DiscountStrategy {

              @Override
              
          public double discount(double sum) 
          {
                  sum 
          = sum * 0.88
          ;
                  
          int t = (int) sum / 400
          ;
                  sum 
          = sum - t * 200
          ;
                  
          return
           sum;
              }

          }

              類Cashier代碼如下:
              
          public class Cashier {
              
          private DiscountStrategy discountStrategy = new
           GeneralDiscountStrategy();

              
          public void getDiscountStrategy(CustomerLevel customerLevel) 
          {
                  
          switch (customerLevel) 
          {
                      
          case
           GENERAL:
                          discountStrategy 
          = new
           GeneralDiscountStrategy();
                          
          break
          ;
                      
          case
           SILVER:
                          discountStrategy 
          = new
           SilverDiscountStrategy();
                          
          break
          ;
                      
          case
           GOLDEN:
                          discountStrategy 
          = new
           GeneralDiscountStrategy();
                          
          break
          ;
                  }

              }


              
          public double calculate(double price, int num) {
                  
          return discountStrategy.discount(price *
           num);
              }

          }
          posted @ 2011-04-06 22:56 simon.shen 閱讀(352) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 泰兴市| 同江市| 南岸区| 济阳县| 安仁县| 娄底市| 信宜市| 永修县| 土默特右旗| 昌图县| 敦化市| 娱乐| 大连市| 昌都县| 南阳市| 乐都县| 江山市| 美姑县| 宁蒗| 泗阳县| 明溪县| 平阴县| 东安县| 桃源县| 雅江县| 惠州市| 寿宁县| 壶关县| 石柱| 咸阳市| 郴州市| 吉林省| 鸡东县| 长岭县| 延长县| 上虞市| 曲周县| 万盛区| 潞城市| 濉溪县| 乌兰县|