星空网 > 软件开发 > ASP.net

设计模式进阶(一) 策略模式

摘自<Design Paterns_Elements of Reusable Object-Oriented Software>

上一系列偏重于入门,从本篇开启进阶系列,着重于设计模式的适用情景。

回顾入门系列 设计模式入门(一)  策略模式

1  Intent

  Define a family of algorithms, encapsulate each one, and make them interchangeable.

  Strategy lets the algorithm vary independently from clients that use it.

  设计模式进阶(一)  策略模式

2  Applicability

1)  many related classes differ only in their behavior

  = strategies provide a way to configure a class with one of many behaviors

2)  you need different variants of an algorithm

  = strategies can be used when these variants are implemented as a class hierarchy of algorithms

3)  an algorithm uses data that clients should not know about

  = use strategies to avoid exposing complex, algorithm-specific data structures

4)  a class defines many behaviors, and these appear as multiple conditional statements in its operations

  = move related conditional branches into their own strategy class

3  Sample

  many algorithms exist for breaking a stream of text into lines, and hard-wiring such algorithms into the class that require them is not desirable.

设计模式进阶(一)  策略模式

  C++ 实例:

1) class Composition

设计模式进阶(一)  策略模式设计模式进阶(一)  策略模式
/* Composition class maintains a collection of Component instances */class Composition {public:  Composition(Compositor*);void Repair();private:  Compositor* _compositor;  Component* _components;  // the list of components  int _componentCount;  // the number of components  int _lineWidth;    // the Composition's line width  int* _lineBreaks;  // the position of linebreaks in components  int _lineCount;    // the number of lines};void Composition::Repair () {  Coord* natural;  Coord* stretchability;  Coord* shrinkability;  int componentCount;  int* breaks;  // prepare the arrays with the desired component sizes  // ...  // determine where the breaks are:  int breakCount;  breakCount = _compositor->Compose(    natural, stretchability, shrinkability,    componentCount, _lineWidth, breaks  );  // lay out components according to breaks  // ...}

View Code

2) class Compositor and its subclasses

设计模式进阶(一)  策略模式设计模式进阶(一)  策略模式
/* Compositor is an abstract class(also interface) */class Compositor{public:  virtual int Compose(    Coord natural[], Coord stretch[],Coord shrink[],    int componentCount, int lineWidth, int breaks[]  ) = 0;protected:  Compositor();};/* Three subclasses */class SimpleCompositor : public Compositor {public:  SimpleCompositor();    virtual int Compose(    Coord natural[], Coord stretch[], Coord shrink[],    int componentCount, int lineWidth, int breaks[]  );  // ...};class TeXCompositor : public Compositor {public:  TeXCompositor();  virtual int Compose(    Coord natural[], Coord stretch[], Coord shrink[],    int componentCount, int lineWidth, int breaks[]  );  // ...};class ArrayCompositor : public Compositor{public:  ArrayCompositor(int interval);    virtual int Compose(    Coord natural[], Coord stretch[], Coord shrink[],    int componentCount, int lineWidth, int breaks[]  );  // ...};

View Code

3) instantiation

设计模式进阶(一)  策略模式设计模式进阶(一)  策略模式
Composition* quick = new Composition(new SimpleCompositor);Composition* slick = new Composition(new TeXCompositor);Composition* iconic = new Composition(new ArrayCompositor(100));

View Code

 




原标题:设计模式进阶(一) 策略模式

关键词:设计模式

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

李宽宽:https://www.goluckyvip.com/tag/37367.html
李龙星:https://www.goluckyvip.com/tag/37368.html
李茂盛:https://www.goluckyvip.com/tag/37369.html
李鹏博:https://www.goluckyvip.com/tag/37371.html
李庆瑜:https://www.goluckyvip.com/tag/37372.html
李绍忠:https://www.goluckyvip.com/tag/37373.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
石象湖景区门票-石象湖景区门票优惠政策:https://www.vstour.cn/a/411243.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流