策略模式demo



/**
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 } }

 

posted @ 2018-12-08 23:30  乙侍  阅读(302)  评论(0编辑  收藏  举报