Skip to content

Strategy Pattern

Edwin Dalorzo edited this page Jan 16, 2018 · 1 revision

Defines a family of algorithms, encapsulates each one, and make them interchangeable. Strategy lets algorithms vary independently from clients that use it.

Readings

Questions

  • What is the intent of the strategy pattern?
  • What are the consequences of the strategy pattern?
  • What are possible indications of the need to use a strategy pattern?
  • How can the context and the concrete strategy share information?
  • How can the behavior of the context be altered dynamically using a strategy pattern?
  • How can conditional statements be eliminated using a strategy pattern?
  • Why is it preferable to use composition over inheritance in a case like this? (See fragile base class issue)
  • What are the advantages/disadvantages of parameter passing? (loose coupling vs unused info)
  • What are the advantages/disadvantages of passing the context? (strong coupling vs require info only)
  • Is there anything that can be done to make the design less coupled when the context is passed as parameter?
  • How can clients be exposed to implementation issue by having to instantiate a concrete strategy?
  • What can be done to deal with an explosion of strategy objects? (See stateless strategies, flyweight pattern)
  • Why is it said that 'using inheritance instead of strategy is harder to maintain, understand and extend? (see Duck problem)
  • What are the main OO principles enforced by the strategy pattern and how?
  • How does the strategy pattern foster the idea of designing for change?
  • Why is the object-aggregation approach to inheritance superior to direct class inheritance for handling variation?
  • What is meant by "switch creep"?
  • What is wrong with copy and paste?
  • Have you ever been in a situation where you did not feel you could afford to anticipate change? What drove you that way? What was the result?
  • Should you ever use switch statements? Why or why not?
  • What does "interface" mean in the statement "program to an interface, not to an implementation"? Do you think it means we are supposed to use something like Java interfaces?
  • What are advantages of composition over inheritance?
  • How can the strategy pattern be implemented in a dynamically-typed programming language like JavaScript?
  • How can the strategy pattern be implemented in a functional programming language?

Optional Readings

Clone this wiki locally