策略模式 Posted on 2021-02-23 | In 设计模式 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253//策略类public abstract class Strategy { public abstract void algorithmInterface();}//策略A 和 策略Bpublic class StrategyA extends Strategy{ @Override public void algorithmInterface() { System.out.println("算法A 的思想"); }}// 策略Bpublic class StrategyB extends Strategy{ @Override 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(); }} Donate comment here Donate WeChat Pay