中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 设计模式
[Behavioral Patterns] The Strategy Pattern.htm
作者:未知 时间:2005-07-24 21:23 出处:JR 责编:chinaitpower
              摘要:[Behavioral Patterns] The Strategy Pattern.htm

 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. 
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有