Chapter 4. Replacing the Algorithm with the Strategy

We kicked off the last chapter by posing the following question: How do you vary part of an algorithm? How can you get step 3 of a five-step process to sometimes do one thing and sometimes do another thing? The answer that we came up with in Chapter 3 is to use the Template Method pattern, to create a base class with a template method that controls the overall processing and then to use subclasses to fill in the details. If we want to sometimes do this and other times to do that, we create two subclasses, one for this and another for that.

Unfortunately, the Template Method pattern has some drawbacks, most of which stem from the fact that this pattern is built around inheritance. As we saw ...

Get Design Patterns in Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.