[关键字]:java,design pattern,设计模式,《Java与模式》学习,Strategy,策略模式,图书,折扣,DiscountStrategy
[环境]:StarUML5.0 + JDK6
[作者]:Winty ([email protected])
[正文]:


package pattern.strategy;

/**
 * 策略模式:Strategy Pattern
 *
 * 图书折扣的计算:
 * 1、无折扣
 * 2、固定折扣(例如:提供10元的折扣)
 * 3、百分比折扣(例如:提供7%的折扣)
 *
 * @verison 2009-05-20
 * @author Winty ([email protected])
 *
 */
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;
    }
}
转载地址: http://www.blogjava.net/wintys/archive/2009/05/21/pattern_strategy.html