Sunday, January 26, 2020

Strategic 0.8.0 - Painless Strategy Pattern in Ruby and Rails

Strategy Pattern is one of those patterns that many developers agree is good but few practically apply in day to day work. As a result, you see codebases littered with hard to maintain if/case conditionals.

This problem is finally solved with the 'strategic' rubygem (Painless Strategy Pattern in Ruby and Rails):


if/case conditionals can get really hairy in highly sophisticated business domains. Domain model inheritance can help remedy the problem, but dumping all logic variations in the same domain models can cause a maintenance nightmare. Thankfully, Strategy Pattern as per the Gang of Four solves the problem by externalizing logic variations to separate classes outside the domain models.

Still, there are a number of challenges with repeated implementation of Strategy Pattern:

  • Making domain models aware of newly added strategies without touching their code (Open/Closed Principle).
  • Fetching the right strategy without use of conditionals.
  • Avoiding duplication of strategy dispatch code for multiple domain models
  • Have different strategies mirror an existing domain model hierarchy

"strategic" solves these problems by offering:

  • Strategy Pattern support through a Ruby mixin and strategy path/name convention
  • Automatic discovery of strategies based on path/name convention
  • Ability to fetch needed strategy without use of conditionals
  • Ability to fetch a strategy by name or by object type to mirror
  • Plain Ruby and Ruby on Rails support
"strategic" enables you to make any existing domain model "strategic", externalizing all logic concerning algorithmic variations into separate strategy classes that are easy to find, maintain and extend.

Learn more on GitHub:

No comments: