[原]策略模式

[关键字]: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值