Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>设计模式>>[Behavioralnbsp;Patterns]nbsp;Thenbsp;Strategynbsp;Pattern.htm
[Behavioralnbsp;Patterns]nbsp;Thenbsp;Strategynbsp;Pattern.htm
2007-04-13   来源:www.javaresearch.org  作者:未知

 Intent 


Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it. 

 Problem 


If clients have potentially generic algorithms embedded in them, it is difficult to: reuse these algorithms, exchange algorithms, decouple different layers of functionality, and vary your choice of policy at run-time. These embedded policy mechanisms routinely manifest themselves as multiple, monolithic, conditional expressions. 

 Structure 


[image]http://javaresearch.org/members/Bryan Lau/Strategy.JPG[/image]
Steps are as follow: 

1. Define the interface of an interchangeable family of algorithms
2. Bury algorithm implementation details in derived classes
3. Derived classes could be implemented using the Template Method pattern
4. Clients of the algorithm couple themselves strictly to the interface

  1. interface Strategy { public void solve(); }          // 1. Define the interface
  2.                                                      //    of the algorithm
  3. abstract class TemplateMethod1 implements Strategy { // 2. Bury implementation
  4.    public void solve() {                             // 3. Template Method 
  5.       start();
  6.       while (nextTry() && ! isSolution())
  7.          ;
  8.       stop();
  9.    }
  10.    protected abstract void    start();
  11.    protected abstract boolean nextTry();
  12.    protected abstract boolean isSolution();
  13.    protected abstract void    stop();
  14. }
  15. class Impl1 extends TemplateMethod1 {
  16.    private int state = 1;
  17.    protected void    start()   { System.out.print( "start  " ); }
  18.    protected void    stop()    { System.out.println( "stop" ); }
  19.    protected boolean nextTry() {
  20.       System.out.print( "nextTry-" + state++ + "  " );
  21.       return true; }
  22.    protected boolean isSolution() {
  23.       System.out.print( "isSolution-" + (state == 3) + "  " );
  24.       return (state == 3);
  25. }  }
  26. abstract class TemplateMethod2 implements Strategy { // 2. Bury implementation
  27.    public void solve() {                             // 3. Template Method
  28.       while (true) {
  29.          preProcess();
  30.          if (search()) break;
  31.          postProcess();
  32.    }  }
  33.    protected abstract void    preProcess();
  34.    protected abstract boolean search();
  35.    protected abstract void    postProcess();
  36. }
  37. class Impl2 extends TemplateMethod2 {
  38.    private int state = 1;
  39.    protected void    preProcess()  { System.out.print( "preProcess  " ); }
  40.    protected void    postProcess() { System.out.print( "postProcess  " ); }
  41.    protected boolean search() {
  42.       System.out.print( "search-" + state++ + "  " );
  43.       return state == 3 ? true : false;
  44. }  }
  45. public class StrategyDemo {    // 4. Clients couple strictly to the interface
  46.    public static void clientCode( Strategy strat ) { strat.solve(); }
  47.    public static void main( String[] args ) {
  48.       Strategy[] algorithms = { new Impl1(), new Impl2() };
  49.       for (int i=0; i < algorithms.length; i++)
  50.          clientCode( algorithms[i] );
  51. }  }
  52. // start  nextTry-1  isSolution-false  nextTry-2  isSolution-true  stop
  53. // preProcess  search-1  postProcess  preProcess  search-2

 When to use 


A program which requires a particular service or function and which has several ways of carrying out that function is a candidate for the Strategy pattern. Programs choose between these algorithms based on computational efficiency or user choice. There can be any number of strategies and more can be added and any of them can be changed at any time. 

The idea behind Strategy is to encapsulate the various strategies in a single module and provide a simple interface to allow choice between these strategies. Each of them should have the same programming interface, although they need not all be members of the same class hierarchy. However, they do have to implement the same programming interface. 


  --相关文章--
· 面向对象编程,我的思想 (2007-04-13)
· 面向对象的思维方式 (2007-04-13)
· 通过Javanbsp;Swing看透MVC设计模式 (2007-04-13)
· 适配器模式(Adapternbsp;Pattern) (2007-04-13)
· 追MM与Java的23种设计模式 (2007-04-13)
· 责任链模式(Chainnbsp;ofnbsp;Responsibility) (2007-04-13)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接