随笔 - 117  文章 - 72  trackbacks - 0

声明:原创作品(标有[原]字样)转载时请注明出处,谢谢。

常用链接

常用设置
常用软件
常用命令
 

订阅

订阅

留言簿(7)

随笔分类(130)

随笔档案(123)

搜索

  •  

积分与排名

  • 积分 - 154340
  • 排名 - 389

最新评论

[关键字]: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.blogjava.net/wintys
posted on 2009-05-21 22:21 天堂露珠 阅读(1298) 评论(3)  编辑  收藏 所属分类: Pattern

FeedBack:
# re: [原]策略模式 2009-05-22 15:43 BruceLeey
字体太大啦  回复  更多评论
  
# re: [原]策略模式 2009-05-22 22:20 天堂露珠
@BruceLeey
哈哈,个人习惯不同。有意把字体调大的,默认字体太小,看着不舒服。  回复  更多评论
  
# re: [原]策略模式 2009-06-01 14:08 邻居家
不错啊,转载了啊  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: