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

设计形式读书笔记之策略模式(Strategy)

2012-10-26 
设计模式读书笔记之策略模式(Strategy) .示例代码:?这个例子很抽象,大多数的设计模式书中都会给出这么个例

设计模式读书笔记之策略模式(Strategy) .

示例代码:

?

这个例子很抽象,大多数的设计模式书中都会给出这么个例子,这个例子不具备任何生产价值,于是我想到了一个更好的例子:
笔记本电脑中都有一个电源管理器,提供多种电源管理方案。每个方案都是一种策略,操作系统(或使用电脑的人)应该能灵活切换供电方案。下面用代码模拟:

?

//策略接口package designpattern.strategy;public interface PowerStrategy {public void apply();}//引用实例,持有策略者package designpattern.strategy;public class Computer {private PowerStrategy ps;public PowerStrategy getPs() {return ps;}public void setPs(PowerStrategy ps) {this.ps = ps;}public void applyPowerStrategy(){ps.apply();}}//策略1 高表现性能策略package designpattern.strategy;public class HighPerformanceStrategy implements PowerStrategy{public void apply() {System.out.println("*** High performance power strategy ***");System.out.println("Turn off monitor: Never;");System.out.println("Turn off hard disk: Never;");System.out.println("System standby: Never;");System.out.println("System hibernates: Never./n");}}//策略2 中等策略package designpattern.strategy;public class MediumStragegy implements PowerStrategy{public void apply() {System.out.println("*** Medium power strategy ***");System.out.println("Turn off monitor: 1 hour;");System.out.println("Turn off hard disk: 2 hours;");System.out.println("System standby: 3 hours;");System.out.println("System hibernates: 4 hours./n");}}//策略3 节能策略package designpattern.strategy;public class EnergySavingStrategy implements PowerStrategy{public void apply() {System.out.println("*** Energy-saving strategy ***");System.out.println("Turn off monitor: 5 minutes;");System.out.println("Turn off hard disk: 15 minutes;");System.out.println("System standby: 30 minutes;");System.out.println("System hibernates: 1 hour./n");}}//测试用例package designpattern.strategy;public class Test {public static void main(String[] args){Computer c = new Computer();c.setPs(new HighPerformanceStrategy());c.applyPowerStrategy();c.setPs(new MediumStragegy());c.applyPowerStrategy();c.setPs(new EnergySavingStrategy());c.applyPowerStrategy();}}

?

经典案例:

1 jdk中的类File有一个方法:public String[] list(FilenameFilter filter),列出当前目录下的所有文件,FilenameFilter就是一个策略接口,使用者可以根据自己的需求自由变换文件筛选策略.
2 jdk中工具类Arrays有一个可以对集合排序的方法: public static <T> void sort(T[] a, Comparator<? super T> c),传入一个数组和一个比较器, 这个比较器就是一个策略.(此案有争议,有人认为是template method模式).

应用场景:
1 多个类只区别在表现行为不同,可以使用Strategy模式,在运行时动态选择具体要执行的行为;
2 需要在不同情况下使用不同的策略(算法)。

优点:
把容易发生变化的算法独立出来,易于扩展。

缺点:

类增多了,使系统难度加大。

class StrategyExample {//test case. 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(); } } // Strategy interface// 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(); }}

热点排行