package design;

public interface PayStrategy {
	public void pay(PayContext  con);
	
}

package design;

public class RmbCash implements PayStrategy {


	@Override
	public void pay(PayContext con) {
		// TODO Auto-generated method stub
		System.out.println("现在用人民币支付"+con.getMoney()+"元");
	}

}

package design;

public class DollarCash implements PayStrategy {

	@Override
	public void pay(PayContext con) {
		// TODO Auto-generated method stub
		System.out.println("现在用美元支付"+con.getMoney()+"元");
	}

}

package design;

public class PayContext {
	private String name=null;
	private Double money;
	private PayStrategy strategy=null;
	
	public PayContext(String name, Double money, PayStrategy strategy) {
		super();
		this.name = name;
		this.money = money;
		this.strategy = strategy;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getMoney() {
		return money;
	}

	public void setMoney(Double money) {
		this.money = money;
	}

	public PayStrategy getStrategy() {
		return strategy;
	}

	public void setStrategy(PayStrategy strategy) {
		this.strategy = strategy;
	}

	public void pay()
	{
		this.strategy.pay(this);
	}

}


package design;

public class MainFive {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PayStrategy strO=new RmbCash();
		PayStrategy strT=new RmbCash();
		PayContext conO=new PayContext("小李",600.0,strO);
		conO.pay();
		PayContext conT=new PayContext("小王",700.0,strT);
		conT.pay();
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值