隨筆 - 117  文章 - 72  trackbacks - 0

          聲明:原創(chuàng)作品(標(biāo)有[原]字樣)轉(zhuǎn)載時(shí)請(qǐng)注明出處,謝謝。

          常用鏈接

          常用設(shè)置
          常用軟件
          常用命令
           

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 156581
          • 排名 - 390

          最新評(píng)論

          [關(guān)鍵字]:java,design pattern,設(shè)計(jì)模式,《Java與模式》學(xué)習(xí),Strategy,策略模式,圖書,折扣,DiscountStrategy
          [環(huán)境]:StarUML5.0 + JDK6
          [作者]:Winty (wintys@gmail.com)
          [正文]:


          package pattern.strategy;

          /**
           * 策略模式:Strategy Pattern
           *
           * 圖書折扣的計(jì)算:
           * 1、無折扣
           * 2、固定折扣(例如:提供10元的折扣)
           * 3、百分比折扣(例如:提供7%的折扣)
           *
           * @verison 2009-05-20
           * @author Winty (wintys@gmail.com)
           *
           */
          public class StrategyTest {
              public static void main(String[] args) {
                  double discount = 0;
                  Book book = new Book(88.5);
                 
                  book.setStrategy(new NoDiscountStrategy());
                  discount = book.getDiscount();
                  System.out.println("折扣:" + discount);
                 
                  book.setStrategy(new FlatDiscountStrategy(10));
                  discount = book.getDiscount();
                  System.out.println("折扣:" + discount);
                 
                  book.setStrategy(new PercentageDiscountStrategy(0.07));
                  discount = book.getDiscount();
                  System.out.println("折扣:" + discount);
              }
          }

          /**
           * 環(huán)境(Context)
           * @author Winty
           *
           */
          class Book{
              private double price;
              private DiscountStrategy strategy;
             
              public Book(double price){
                  this.price = price;
              }

              /**
               * 調(diào)用Strategy的策略方法進(jìn)行計(jì)算
               * @return 折扣數(shù)額
               */
              public double getDiscount(){
                  strategy.setPrice(price);

                  return strategy.calculateDiscount();
              }
             
              public DiscountStrategy getStrategy() {
                  return strategy;
              }

              public void setStrategy(DiscountStrategy strategy) {
                  this.strategy = strategy;
              }
             
              public double getPrice() {
                  return price;
              }

              public void setPrice(double price) {
                  this.price = price;
              }
          }

          /**
           * 抽象策略:Strategy
           * @author Winty
           *
           */
          abstract class DiscountStrategy{
              private double price;
             
              public DiscountStrategy(){
              }
             
              /**
               * 策略方法
               * @return 折扣數(shù)額
               */
              public abstract double calculateDiscount();

              public double getPrice() {
                  return price;
              }

              public void setPrice(double price) {
                  this.price = price;
              }
          }

          /**
           * 具體策略:無折扣算法
           * @author Winty
           *
           */
          class NoDiscountStrategy extends DiscountStrategy{
              public NoDiscountStrategy(){
              }
             
              @Override
              public double calculateDiscount(){
                  return 0.0;
              }
          }

          /**
           * 具體策略:固定量折扣算法
           * @author Winty
           *
           */
          class FlatDiscountStrategy extends DiscountStrategy{
              private double amount;
             
              public FlatDiscountStrategy(double amount){
                  this.amount = amount;
              }
             
              @Override
              public double calculateDiscount() {
                  return amount;
              }

              public double getAmount() {
                  return amount;
              }

              public void setAmount(double amount) {
                  this.amount = amount;
              }
          }

          /**
           * 具體策略:百分比折扣策略
           * @author Winty
           *
           */
          class PercentageDiscountStrategy extends DiscountStrategy{
              private double percent;
             
              public PercentageDiscountStrategy(double percent){
                  this.percent = percent;
              }

              @Override
              public double calculateDiscount() {
                  return getPrice()*percent;
              }

              public double getPercent() {
                  return percent;
              }

              public void setPercent(double percent) {
                  this.percent = percent;
              }
          }

          原創(chuàng)作品,轉(zhuǎn)載請(qǐng)注明出處。
          作者:Winty (wintys@gmail.com)
          博客:http://www.aygfsteel.com/wintys
          posted on 2009-05-21 22:21 天堂露珠 閱讀(1313) 評(píng)論(3)  編輯  收藏 所屬分類: Pattern

          FeedBack:
          # re: [原]策略模式 2009-05-22 15:43 BruceLeey
          字體太大啦  回復(fù)  更多評(píng)論
            
          # re: [原]策略模式 2009-05-22 22:20 天堂露珠
          @BruceLeey
          哈哈,個(gè)人習(xí)慣不同。有意把字體調(diào)大的,默認(rèn)字體太小,看著不舒服。  回復(fù)  更多評(píng)論
            
          # re: [原]策略模式 2009-06-01 14:08 鄰居家
          不錯(cuò)啊,轉(zhuǎn)載了啊  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 广河县| 潜山县| 花莲县| 兴仁县| 淳化县| 惠安县| 门头沟区| 望江县| 保山市| 赤城县| 凤凰县| 瑞丽市| 平定县| 阿拉善右旗| 莱阳市| 金乡县| 望城县| 平远县| 阳谷县| 分宜县| 南汇区| 锡林郭勒盟| 板桥市| 铜梁县| 泰安市| 晋中市| 万州区| 会昌县| 宜兰县| 远安县| 临夏县| 邵阳市| 呼图壁县| 江川县| 连南| 黎川县| 绵阳市| 华亭县| 赞皇县| 南漳县| 烟台市|