Design mode Advanced (one) policy mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.