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

策略模式

时间:2017-06-15 10:35:07      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:str   this   ide   port   author   nes   mon   shc   void   

CashCollect.java类

package strategy;

import java.math.BigDecimal;

public abstract class CashCollect {
    public abstract BigDecimal cashCollect(BigDecimal total);
    
}

DisCountCashCollect.java类

package strategy;

import java.math.BigDecimal;

public class DisCountCashCollect extends CashCollect {
    private BigDecimal discount;
    
    public DisCountCashCollect(BigDecimal discount) {
        super();
        this.discount = discount;
    }

    @Override
    public BigDecimal cashCollect(BigDecimal total) {
        System.out.println(total);
        System.out.println(discount);
        return total.multiply(discount);
    }

}

NormalCashCollect.java类

package strategy;

import java.math.BigDecimal;

public class NormalCashCollect extends CashCollect {

    @Override
    public BigDecimal cashCollect(BigDecimal total) {
        return total;
    }

}

RebateCashCollect.java类

package strategy;

import java.math.BigDecimal;

public class RebateCashCollect extends CashCollect {
    private BigDecimal fullMoney;
    private BigDecimal returnMoney;
    
    public RebateCashCollect(BigDecimal fullMoney, BigDecimal returnMoney) {
        super();
        this.fullMoney = fullMoney;
        this.returnMoney = returnMoney;
    }

    @Override
    public BigDecimal cashCollect(BigDecimal total) {
        if(fullMoney.compareTo(total)>=0){
            total = total.subtract(returnMoney);
        }
        return total;
    }

}

CashContext.java类

package strategy;

import java.math.BigDecimal;

public class CashContext {
    CashCollect cc ;

    /*public CashContext(CashCollect cc) {
        super();
        this.cc = cc;
    }*/
    public CashContext(String type){
        switch(type){
            case "原价":
                cc = new NormalCashCollect();
                break;
            case "打8折":
                cc = new DisCountCashCollect(new BigDecimal("0.8"));
                break;
            case "满300返100":
                cc = new RebateCashCollect(new BigDecimal("300"), new BigDecimal("100"));
                break;
        }
    }
    public BigDecimal cashCollect(BigDecimal total ){
        return cc.cashCollect(total);
    }
    
    
}

BusinessStrategy.java类

package strategy;

import java.math.BigDecimal;
/**
 * 策略模式
 * @author 煞笔
 *
 */

public class BusinessStrategy {

    public static void main(String[] args) {
        //BigDecimal total = new BigDecimal("1000");
        CashContext cct = new CashContext("打8折");
        //cct.total = total;
        BigDecimal total = cct.cashCollect(new BigDecimal("1000"));
        System.out.println(total);
    }

}

 

策略模式

标签:str   this   ide   port   author   nes   mon   shc   void   

原文地址:http://www.cnblogs.com/ccgjava/p/7015548.html

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