Chapter 3. Varying the Algorithm with the Template Method

Imagine that you have a complex bit of code—maybe an involved algorithm or just some hairy system code or perhaps something just hard enough that you want to code it once, write some unit tests, and leave it alone. The trouble is that somewhere right in the middle of your complex code is a bit that needs to vary. Sometimes this bit of code wants to do this and sometimes it wants to do that. Even worse, you are pretty sure that in the future the silly thing will need to something else. You are up against the old “defend against change” problem discussed in Chapter 1. What do you do?

To make this scenario more concrete, imagine that your first real Ruby project is to write a report generator—that ...

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.