策略模式
为所有的算法定义一个抽象的算法接口,并通过继承该接口对所有的算法加以封装和实现,具体的算法选择交由客户端决定。(【1】把变化的代码从不变的代码中分离出来;【2】针对接口编程,而不是具体类;【3】将继承换成组合或聚合)
客户context有成员变量strategy(或者其他的策略接口),至于需要使用到哪一个策略,可以在构造器中指定。
package strategy;
public interface Flybehavior {
void fly();//子类具体实现
}
package strategy;
public class GoodFly implements Flybehavior {
public void fly() {
// TODO Auto-generated method stub
System.out.println("飞行能力好");
}
}
package strategy;
public class BadFly implements Flybehavior {
public void fly() {
// TODO Auto-generated method stub
System.out.println("飞行能力不好");
}
}
package strategy;
public abstract class Duck {
//注意抽象类里必须有抽象方法,也可以有非抽象方法
//属性,策略接口
Flybehavior flybehavior;
public void fly() {
if(flybehavior!=null)//属性不为空,说明已经把具体子类给了它
flybehavior.fly();
}
public abstract void show();
public Duck() {
super();
// TODO Auto-generated constructor stub
}
}
package strategy;
public class ToyDuck extends Duck {
//构造器传入Flybehavior的对象
public ToyDuck() {
// TODO Auto-generated constructor stub
flybehavior=new BadFly();
}
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("玩具鸭");
}
}
package strategy;
public class WildDuck extends Duck {
public void show() {
System.out.println("野鸭");
}
//构造器传入Flybehavior的对象
public WildDuck() {
super();
// TODO Auto-generated constructor stub
flybehavior=new GoodFly();
}
}
package strategy;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
WildDuck a=new WildDuck();
a.fly();
ToyDuck b=new ToyDuck();
b.fly();
}
}
package zhifu;
public interface PaymentStrategy {
void pay(PaymentContext ctx);//把上下文作为参数传递给策略对象
}
package zhifu;
public class PaymentContext {
private String name=null;
private double salary=0.0;
private PaymentStrategy strategy=null;
public PaymentContext(String name, double salary, PaymentStrategy strategy) {
super();
this.name = name;
this.salary = salary;
this.strategy = strategy;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public void payNow(){
this.strategy.pay(this);
}
}
package zhifu;
public class RMBCash implements PaymentStrategy {
public RMBCash() {
// TODO Auto-generated constructor stub
}
@Override
public void pay(PaymentContext ctx) {
// TODO Auto-generated method stub
System.out.println("人民币支付:"+ctx.getName()+" "+ctx.getSalary());
}
}
package zhifu;
public class DollarCash implements PaymentStrategy {
public DollarCash() {
}
@Override
public void pay(PaymentContext ctx) {
// TODO Auto-generated method stub
System.out.println("美元支付:"+ctx.getName()+" "+ctx.getSalary());
}
}
package zhifu;
public class Card implements PaymentStrategy {
public Card() {
// TODO Auto-generated constructor stub
}
@Override
public void pay(PaymentContext ctx) {
// TODO Auto-generated method stub
System.out.println("信用卡支付:"+ctx.getName()+" "+ctx.getSalary());
}
}
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建相应的支付策略
PaymentStrategy rmb=new RMBCash();
PaymentStrategy dollar=new DollarCash();
PaymentStrategy card=new Card();
PaymentContext ctx1=new PaymentContext("li",30000,rmb);
ctx1.payNow();
PaymentContext ctx2=new PaymentContext("zhang",22000,dollar);
ctx2.payNow();
PaymentContext ctx3=new PaymentContext("wang",30800,card);
ctx3.payNow();
}
}