首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

计策模式 [取自wikipedia]

2012-06-30 
策略模式 [取自wikipedia]策略模式作为一种软件设计模式,指对象有某个行为,但是在不同的场景中,该行为有不

策略模式 [取自wikipedia]
策略模式作为一种软件设计模式,指对象有某个行为,但是在不同的场景中,该行为有不同的实现算法。比如每个人都要“交个人所得税”,但是“在美国交个人所得税”和“在中国交个人所得税”就有不同的算税方法。

//StrategyExample test application class StrategyExample {     public static void main(String[] args) {         Context context;         // Three contexts following different strategies        context = new Context(new FirstStrategy());        context.execute();         context = new Context(new SecondStrategy());        context.execute();         context = new Context(new ThirdStrategy());        context.execute();     } } // The classes that implement a concrete strategy should implement this // The context class uses this to call the concrete strategyinterface Strategy {     void execute(); } // Implements the algorithm using the strategy interfaceclass FirstStrategy implements Strategy {     public void execute() {        System.out.println("Called FirstStrategy.execute()");    } } class SecondStrategy implements Strategy {     public void execute() {        System.out.println("Called SecondStrategy.execute()");    } } class ThirdStrategy implements Strategy {     public void execute() {        System.out.println("Called ThirdStrategy.execute()");    } } // Configured with a ConcreteStrategy object and maintains a reference to a Strategy objectclass Context {     Strategy strategy;     // Constructor    public Context(Strategy strategy) {        this.strategy = strategy;    }     public void execute() {        this.strategy.execute();    } }

热点排行