Strategy decorator

Printer-friendly versionPrinter-friendly version
Decorators over strategies

 

Solution

  • We decompose strategies into their operations, so that each strategy operation can be implemented in a separate class.
  • We make it possible to chain implementations of the same strategy interface in any combination and in any order.
  • We make it possible to add and use strategy operations without an implementation for the respective operation.
  • We implement a generic default behavior once and for all invocations of strategy operations where no other behavior applies.

 

Class Diagram I

 

Class Diagram I Benefits

This design allows to

  • select algorithms in any combination and in any order, separately for any operation of the strategy interface and determined by any number, any type and any combination of parameters.
  • apply behavior hierarchically where it is needed, with an unlimited depth of the hierarchy, for instance if the parameters relate to each other hierarchically.
  • to choose the same behavior for different sets of parameter values or for different parameter types, without having to duplicate the implementation of that behavior.
  • to parallelize the work easily, since the implementations will be much smaller pieces of code and decoupled from each other.
  • add operations to the strategy interface without having to add or change any strategy or decorator. Just the forwarding to the decorated strategy has to be added.
  • automatically apply a generic default behavior where no other behavior has been added. A strategy operation can be used even without any decorator for that operation.
  • generate a proxy for each decorated strategy in order to apply AOP behavior for cross-cutting concerns like logging or authorization.

 

Class Diagram I Principles

The Strategy Decorator design pattern adheres to the SOLID principles:

  • Single-Responsibility Principle (SRP)
  • Open-Closed Principle (OCP)
  • Liskov Substitution Principle (LSP)
  • Interface Segregation Principle (ISP)
  • Dependency Inversion Principle (DIP)

 

As well as to the following principles:

  • Composition over Inheritance (CoI)
  • Don‘t Repeat Yourself (DRY)

 

Pages