码迷,mamicode.com
首页 > 其他好文 > 详细

strategy(策略模式)

时间:2014-05-15 02:23:17      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

bubuko.com,布布扣

DiscountStrategy.java

bubuko.com,布布扣
package edu.strategy;

abstract public class DiscountStrategy
{
    abstract public double calculateDiscount();
}
bubuko.com,布布扣

FlatRateStrategy.java

bubuko.com,布布扣
package edu.strategy;

public class FlatRateStrategy extends DiscountStrategy
{
    private double amount;
    private double price = 0;
    private int copies = 0;

    public FlatRateStrategy(double price, int copies)
    {
        this.setPrice(price);
        this.copies = copies;
    }

    public double getAmount()
    {
        return amount;
    }

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

    public double calculateDiscount()
    {
        return copies * amount;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
bubuko.com,布布扣

NoDiscountStrategy.java

bubuko.com,布布扣
package edu.strategy;

public class NoDiscountStrategy extends DiscountStrategy {
    private double price = 0.0;
    private int copies = 0;

    public double getPrice() {
        return price;
    }

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

    public int getCopies() {
        return copies;
    }

    public void setCopies(int copies) {
        this.copies = copies;
    }

    public NoDiscountStrategy(double price, int copies) {
        this.price = price;
        this.copies = copies;
    }

    public double calculateDiscount() {
        return 0.0;
    }
}
bubuko.com,布布扣

PercentageStrategy.java

bubuko.com,布布扣
package edu.strategy;

public class PercentageStrategy extends DiscountStrategy
{
    private double percent = 0.0;
    private double price = 0.0;
    private int copies = 0;

    public PercentageStrategy(double price, int copies)
    {
        this.price = price;
        this.copies = copies;
    }

    public double getPercent()
    {
        return percent;
    }

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

    public double calculateDiscount()
    {
        return copies * price * percent;
    }

}
bubuko.com,布布扣

调用如下

bubuko.com,布布扣

 

Context.java

bubuko.com,布布扣
package edu.strategy.test;

public class Context {
    private Strategy strategy;
    /*
     * 策略方法
     */
    public void contextInterface(){
        strategy.strategyInterface();
    }
}
bubuko.com,布布扣

ContextStrategy.java

bubuko.com,布布扣
package edu.strategy.test;

public class ContextStrategy extends Strategy {
    /*
     * 策略方法
     * 
     * @see edu.strategy.Strategy#strategyInterface()
     */
    @Override
    public void strategyInterface() {

    }

}
bubuko.com,布布扣

Strategy.java

bubuko.com,布布扣
package edu.strategy.test;

abstract public class Strategy {
    /*
     * 策略方法
     */
    public abstract void strategyInterface();
}
bubuko.com,布布扣

 

 

 

strategy(策略模式),布布扣,bubuko.com

strategy(策略模式)

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/jianfengyun/p/3728733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!