Excerpted from <design paterns_elements of reusable object-oriented software>
A series of emphasis on the introduction, from this chapter to open the Advanced series, focusing on the design pattern of the applicable scenarios.
Review introductory series Introduction to design Patterns (i) Policy mode
1 Intent
Define A family of algorithms, encapsulate each one, and make them interchangeable.
Strategy lets the algorithm vary independently from clients.
2 Applicability
1) Many related classes differ only in their behavior
= Strategies provide a-configure a class with one of many behaviors
2) You need different variants of a algorithm
= strategies can used when these variants is implemented as a class hierarchy of algorithms
3) An algorithm uses data, 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 to lines, and hard-wiring such algorithms into the class that Requir E them is not desirable.
C + + instances:
1) Class composition
/*composition class maintains a collection of Component instances*/classComposition { Public: Composition (compositor*);voidRepair ();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};voidComposition::repair () {Coord*Natural; Coord*stretchability; Coord*shrinkability; intComponentcount; int*breaks; //prepare the arrays with the desired component sizes// ... //determine where the breaks is: intBreakcount; 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)*/classcompositor{ Public: Virtual intCompose (Coord natural[], Coord Stretch[],coord shrink[],intComponentcount,intLineWidth,intbreaks[])=0;protected: Compositor ();};/*three subclasses*/classSimplecompositor: Publiccompositor { Public: Simplecompositor (); Virtual intCompose (Coord natural[], Coord stretch[], Coord shrink[],intComponentcount,intLineWidth,intbreaks[]); // ...};classTexcompositor: Publiccompositor { Public: Texcompositor (); Virtual intCompose (Coord natural[], Coord stretch[], Coord shrink[],intComponentcount,intLineWidth,intbreaks[]); // ...};classArraycompositor: Publiccompositor{ Public: Arraycompositor (intinterval); Virtual intCompose (Coord natural[], Coord stretch[], Coord shrink[],intComponentcount,intLineWidth,intbreaks[]); // ...};
View Code
3) instantiation
New Composition (new simplecompositor); Compositionnew composition (new texcompositor); Compositionnew composition (new arraycompositor);
View Code
Design mode Advanced (one) policy mode