Strategy Pattern

The Strategy Pattern defines a family of algorithms, encapsulates each one and makes them interchangeable. Strategy lets algorithm vary independently from clients that use it.

Bates and SierraHead First Design Patterns

##Class diagram


##Design Principles Some design principle can help in the definition and explanation of the pattern.

  • Encapsulate what varies.
    • Encapsulating what varies don’t affect other parts of the code when changes.
    • Because of new requirements some aspects of the application could change quite often. Aspects mean behaviors and they have to be separated from aspects that don’t change.
    • Base scheme for all design patterns, they allow some part of the system to vary independently of all other parts.
  • Program to an interface, not an implementation.
    • Decouple the code from a specific implementation.
    • Allow to change the implementation at runtime.
  • Favor composition over inheritance.
    • Better to get behaviors by composition than by inheritance.
    • Composition gives more flexibility.
    • Encapsulate family of algorithms or behaviors into their own set of classes.
    • Allow to change behavior at runtime.

##Scenario A product could update quite frequently because of new requirements introducing new behavior. Considering a simple class inheritance is the first solution that comes to mind.

A class defines some variables to define the state of an instance and methods to implement behaviors. Focus on behaviors.

  • Subclassing allows to inherit behaviors which could change across subclasses.
  • Overriding behaviors must be done subclass by subclass.
  • Subclass may inherit useless behaviors.

    So how to improve code reuse, avoid code duplication and make simple the design?

##Design Principles in Action To better design the application apply the design principles explained so far.

  • Encapsulate what varies.
    • Create one or more set of classes to encapsulate the implementation of their respective behaviors.
    • Behaviors encapsulated into classes can be reused.
    • Encapsulation means put a given implementation in a separated class.
    • Separation and encapsulation allow composition, so behaviors are assigned to instances.
  • Program to an interface.
    • Implementation can be defined at runtime, no more specific implementation.
    • Interfaces are a set of behaviors, some classes exist only to implement a behavior.
    • Inheritance. Synonym of concrete implementation. An inherited behavior means concrete implementation from the superclass or the subclass providing a specialized implementation of the behavior.
    • Interface. Synonym of a set of behaviors.
      • Program to an abstract supertype. Program to an interface or abstract class.
      • Polymorphism. The runtime object or actual implementation of the behavior is not locked into the client class.
  • Favorite composition.
    • HAS-A relation.
      • It means delegation, a class delegate its behavior to other classes instead of defining them by itself, IS-A.
      • It is better than IS-A because it avoids to lock the client to a specific implementation and change behaviors at runtime using interfaces.
      • Behaviors can be changed without affecting the clients, new behaviors can be added without touching the existing ones.
      • Behaviors can be reused by multiple clients.

##Set Behavior Implementation The behavior implementation can be set via

  • constructor: once the class is created the behavior class is set,
  • setter method: call the setter any time to change the class behavior on the fly,
  • mixed: use the constructor to set a default behavior and the setter to change it at runtime,
  • subclass: can define a new constructor.

Set of behaviors can be seen as a family of algorithms.



Composition is realized by defining an interface type variable and then delegating to different set of classes the implementation of the behaviors.


Behaviors defined by composition are implemented by external classes which exist only for this purpose, so they can change, extended and added without affecting Person class.