隨筆 - 117  文章 - 72  trackbacks - 0

          聲明:原創作品(標有[原]字樣)轉載時請注明出處,謝謝。

          常用鏈接

          常用設置
          常用軟件
          常用命令
           

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 155796
          • 排名 - 390

          最新評論

          [關鍵字]:java,design pattern,設計模式,《Java與模式》學習,Strategy,策略模式,圖書,折扣,DiscountStrategy
          [環境]:StarUML5.0 + JDK6
          [作者]:Winty (wintys@gmail.com)
          [正文]:


          package pattern.strategy;

          /**
           * 策略模式:Strategy Pattern
           *
           * 圖書折扣的計算:
           * 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);
              }
          }

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

              /**
               * 調用Strategy的策略方法進行計算
               * @return 折扣數額
               */
              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 折扣數額
               */
              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;
              }
          }

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

          FeedBack:
          # re: [原]策略模式 2009-05-22 15:43 BruceLeey
          字體太大啦  回復  更多評論
            
          # re: [原]策略模式 2009-05-22 22:20 天堂露珠
          @BruceLeey
          哈哈,個人習慣不同。有意把字體調大的,默認字體太小,看著不舒服。  回復  更多評論
            
          # re: [原]策略模式 2009-06-01 14:08 鄰居家
          不錯啊,轉載了啊  回復  更多評論
            
          主站蜘蛛池模板: 饶河县| 绍兴市| 法库县| 淮北市| 宣武区| 上虞市| 灌云县| 常德市| 长顺县| 尼玛县| 乌兰察布市| 青浦区| 朝阳县| 文安县| 丽江市| 贵港市| 平湖市| 新闻| 博白县| 康保县| 九龙坡区| 吉林市| 炎陵县| 天祝| 东平县| 车致| 石渠县| 扬州市| 湟中县| 泌阳县| 离岛区| 霍邱县| 翁源县| 澎湖县| 连城县| 会昌县| 辽阳县| 康保县| 祁连县| 水富县| 泰宁县|