Strategy and Decorator Design Patterns in Ruby

Micah Shute
5 min readFeb 28, 2019

Why do I use a Decorator or Strategy?

Or any design pattern, for that matter? In general, design patterns are created and used to promote object composition over inheritance, allow for (black-box) code reuse, better enforce the single responsibility principle, and reduce the size and unwieldiness of your code’s classes in general.

Like any design pattern, Decorators and Strategies have specific scenarios when they are there are beneficial to use. These two patterns can have similar use cases, and it can be easy to confuse when you should use one or the other. According to [1], both Decorators and Strategies can be employed when your goal is an “alternative to extending functionality by subclassing”. Strategies can also be used when you have classes with algorithmic dependencies, and Decorators can be used to easily alter the functionality of a class which itself is not easily changed.

So what actually are they and how do you use them?

Let’s get specific. Decorators and Strategies are just extra classes I write to alter/extend the functionality of a class I have already written. The way they alter this functionality is different, and therefore they are useful in different scenarios. First, let’s look at the class diagrams [1]:

Decorator

Decorator Class Diagram

--

--

Micah Shute

Computer and Software Engineer. Former Nuclear Submarine Officer, USNA ’12.