//策略类 public abstract class Strategy { public abstract void algorithmInterface (); } //策略A 和 策略B public class StrategyA extends Strategy { @Override public void algorithmInterface () { System . out . println ( "算法A 的思想
public abstract class Strategy {
public abstract void algorithmInterface();
}
//策略A 和 策略B
public class StrategyA extends Strategy{
public void algorithmInterface() {
System.out.println("算法A 的思想");
}
}
// 策略B
public class StrategyB extends Strategy{
public void algorithmInterface() {
System.out.println("算法B 的思想");
}
}
// 策略的执行对象
public class Context {
Strategy strategy;
public Context(Strategy strategy){
this.strategy = strategy;
}
public void contextInterFace(){
strategy.algorithmInterface();
}
}
//客户端
public class Client {
public static void main(String[] args) {
Context context;
context = new Context(new StrategyA());
context.contextInterFace();
context = new Context(new StrategyB());
context.contextInterFace();
}
}