// 策略模式 // 定义一个抽象类 public abstract class Strategy { public abstract void algorithm(); } // 定义具体的策略 public class ConcreteStrategyA extends Strategy { public void algorithm() { System.out.println("使用策略A"); } } public class ConcreteStrategyB extends Strategy { public void algorithm() { System.out.println("使用策略B"); } } // 定义上下文类 public class Context { private Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } public void executeAlgorithm() { strategy.algorithm(); } } // 测试类 public class Test { public static void main(String[] args) { Context context; context = new Context(new ConcreteStrategyA()); context.executeAlgorithm(); context = new Context(new ConcreteStrategyB()); context.executeAlgorithm(); } }
标签:策略,void,模式,strategy,context,Context,Strategy,public From: https://www.cnblogs.com/deepalley/p/17105097.html