活到老,學(xué)到老

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

          2011年4月5日 #

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

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

               摘要:     這篇寫一個簡單的HelloWorld例子。     首先準(zhǔn)備環(huán)境,我使用的JDK1.6,1.5應(yīng)該也可以。還需要去oracle下載JMX RI包,地址為:http://www.oracle.com/technetwork/java/javase/tech/download-jsp-141676.html,下載...  閱讀全文
          posted @ 2011-04-05 16:20 simon.shen 閱讀(2964) | 評論 (1)編輯 收藏

          主站蜘蛛池模板: 伊金霍洛旗| 木兰县| 大足县| 宜都市| 治多县| 精河县| 乌拉特中旗| 荆州市| 黄冈市| 龙游县| 聂拉木县| 福泉市| 罗田县| 张掖市| 阳谷县| 赞皇县| 冷水江市| 北京市| 新邵县| 晋宁县| 杭锦旗| 鹿邑县| 大冶市| 丹江口市| 德江县| 汾阳市| 鲜城| 贵南县| 额济纳旗| 古蔺县| 浦县| 绵阳市| 临安市| 武川县| 昭平县| 武隆县| 井陉县| 云和县| 呈贡县| 朝阳区| 龙川县|