读书人

举动型Strategy(策略模式)

发布时间: 2012-10-31 14:37:32 作者: rapoo

行为型——Strategy(策略模式)

?

?举动型——Strategy(策略模式)

?

package strategy;public abstract class RepTempRule {public abstract void replace() throws Exception;}

?

package strategy;public class RepTempRuleOne extends RepTempRule {public void replace() throws Exception {System.out.println("RepTempRuleOne");}}

?

package strategy;public class RepTempRuleTwo extends RepTempRule {public void replace() throws Exception {System.out.println("RepTempRuleTwo");}}

?

package strategy;public class TestStrategy {RepTempRule rule = null;public TestStrategy(RepTempRule rule){this.rule = rule;}public void doStrategy(){try {rule.replace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {RepTempRule tempRule = new RepTempRuleOne();TestStrategy test = new TestStrategy(tempRule);test.doStrategy();}}

?

读书人网 >软件架构设计

热点推荐