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();
}
}