/** StrategyContext. */
public class StrategyContext {
private Strategy strategy;
// 传入的是Strategy的实现类的实例
public Context(Strategy strategy){
this.strategy = strategy;
}
public void contextMethod(){
// 这是关键
strategy.strategyMethod();
}
}
/** 策略接口. */
public interface Strategy {
public void strategyMethod();
}
/** 策略A. */
public class StrategyA implements Strategy {
@Override
public void strategyMethod() {
//业务A
}
}
public class StrategyB implements Strategy {
@Override
public void strategyMethod() {
//业务B
}
}
public class StrategyC implements Strategy {
@Override
public void strategyMethod() {
//业务C
}
}