The Strategy Pattern Flashcards

1
Q

What is the intent of the Strategy Pattern ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the motivation of the Strategy Pattern ?

A

Many algorithms exist for breaking a stream of text into lines. Hard-wiring all such algorithms into the classes that require them isn’t desirable for several reasons:

  • Clients that need linebreaking get more complex if they include the line-breaking code. That makes clients bigger and harder to maintain, especially if they support multiple linebreaking algorithms.
  • Different algorithms will be appropriate at different times. We don’t want to support multiple linebreaking algorithms if we don’t use them all.
  • It’s difficult to add new algorithms and vary existing ones when linebreaking is an integral part of a client.

We can avoid these problems by defining classes that encapsulate different line-breaking algorithms. An algorithm that’s encapsulated in this way is called a strategy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the applicability of the Strategy Pattern ?

A

Use the Strategy pattern when

  • many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors.
  • you need different variants of an algorithm. For example, you might define algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms [HO87].
  • an algorithm uses data that clients shouldn’t know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures.
  • a class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the participants of the Strategy Pattern structure ?

A
  • The Strategy Interface
  • The Concrete Strategies
  • The Context
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the role of the concrete strategies participants in the Strategy pattern structure ?

A

implements the algorithm using the Strategy interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the role of the strategy interface participant in the Strategy pattern structure ?

A

declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the role of the Context participant in the Strategy pattern structure

A
  • is configured with a ConcreteStrategy object.

– maintains a reference to a Strategy object.

– may define an interface that lets Strategy access its data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe the collaborations between the Strategy pattern participants ?

A
  • Strategy and Context interact to implement the chosen algorithm. A context may pass all data required by the algorithm to the strategy when the algorithm is called. Alternatively, the context can pass itself as an argument to Strategy operations. That lets the strategy call back on the context as required.
  • A context forwards requests from its clients to its strategy. Clients usually create and pass a ConcreteStrategy object to the context; thereafter, clients interact with the context exclusively. There is often a family of ConcreteStrategy classes for a client to choose from.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the benefits and drawbacks of the Strategy Pattern ?

A
  1. Families of related algorithms.
  2. An alternative to subclassing.
  3. Strategies eliminate conditional statements.
  4. A choice of implementations.
  5. Clients must be aware of different Strategie
  6. Communication overhead between Strategy and Context.
  7. Increased number of objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the implementation challenges/issues of the implementation of the strategy pattern ?

A
  • Defining the Strategy and Context interfaces. Specifically how data are passed between both.
  • Strategies as template parameters.if (1) the Strategy can be selected at compile-time, and (2) it does not have to be changed at run-time.
  • Making Strategy objects optional. The Context class may be simplified if it’s meaningful not to have a Strategy object. Context checks to see if it has a Strategy object before accessing it. If there is one, then Context uses it normally. If there isn’t a strategy, then Context carries out default behavior.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly