活到老,學(xué)到老

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

          2011年4月5日 #

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

               摘要:     這篇寫一個簡單的HelloWorld例子。     首先準備環(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 閱讀(2968) | 評論 (1)編輯 收藏

          主站蜘蛛池模板: 阿尔山市| 崇信县| 屯留县| 锦州市| 巨鹿县| 沙雅县| 广宗县| 新野县| 彝良县| 兴和县| 胶州市| 达孜县| 顺平县| 武乡县| 鞍山市| 靖宇县| 万盛区| 罗山县| 平南县| 宜都市| 蒙阴县| 庄河市| 彩票| 纳雍县| 济宁市| 利川市| 尚志市| 康马县| 湖北省| 屯昌县| 海门市| 台南市| 松溪县| 行唐县| 井冈山市| 来宾市| 张掖市| 大厂| 古交市| 淄博市| 平潭县|