大话设计模式2 策略模式

strategymode.java

package org.ustc.strategy;

public class StrategyMode {
	private CashSuper cs = null ;

	public CashSuper getCs() {
		return cs;
	}

	public void setCs(CashSuper cs) {
		this.cs = cs;
	}
	public StrategyMode(String cash) {
		// TODO Auto-generated constructor stub
		char letter = cash.charAt(0);
		switch(letter){
		case '正':
			cs = new CashNormal();
			break;
		case '满':
			cs = new CashReturn(300,100);
		break;
		case '打':
			cs = new CashDazhe(0.8);
			break;
		}
	}
	
	public double getResult(int money){
		return cs.acceptCash(money);
	}
}


cashSuper.java

package org.ustc.strategy;

public interface CashSuper {
	public double acceptCash(int money);
}

实现类

package org.ustc.strategy;

public class CashDazhe implements CashSuper {
	
	private double zhekou ;
	//打折
	public CashDazhe(double zhekou) {
		super();
		this.zhekou = zhekou;
	}

	public double getZhekou() {
		return zhekou;
	}

	public void setZhekou(double zhekou) {
		this.zhekou = zhekou;
	}

	@Override
	public double acceptCash(int money) {
		// TODO Auto-generated method stub
		return money*zhekou;
	}

}

package org.ustc.strategy;

public class CashNormal implements CashSuper {

	@Override
	public double acceptCash(int money) {
		// TODO Auto-generated method stub
		return money;
	}

}


package org.ustc.strategy;

public class CashReturn implements CashSuper {
	private int man;
	private int huan;
	//满?还?
	public CashReturn(int man, int huan) {
		super();
		this.man = man;
		this.huan = huan;
	}

	public int getMan() {
		return man;
	}

	public void setMan(int man) {
		this.man = man;
	}

	public int getHuan() {
		return huan;
	}

	public void setHuan(int huan) {
		this.huan = huan;
	}

	@Override
	public double acceptCash(int money) {
		// TODO Auto-generated method stub
		return money - money/man*huan;
	}

}

main.java

package org.ustc.strategy;

public class Main {
	public static void main(String[] args) {
		StrategyMode str = new StrategyMode("满300反100");
		double cash = str.getResult(1000);
		System.out.println("应付款项"+cash);
	}
	
	
}
 UML



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值