行为型——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();}}
?