O'Reilly logo

Design Patterns in Ruby by Russ Olsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required