Design Patterns in Ruby

Reviewed April 2nd, 2009 @ 09:20AM

[Book Cover] Design Patterns in Ruby

If you have been programming for any extended period of time, I am sure you have started to see different patterns emerging out of your tasks. You may find you are doing the same thing over and over again, solving problems that you had previously solved in another project. These patterns can vary from smaller chunks of re-usable code, to manage the sending and receiving of email, all the way to watching over a part of your application and triggering notifications to another part of your application. This book, Design Patterns in Ruby takes several of the principles found in the widely known book, Design Patterns by The Gang of Four, and applies them to Ruby. It's a mixture of the theory behind the design patterns, and then hands-on practice applying it to your Ruby applications. While this book is centered around patterns in Ruby, it is not about teaching you Ruby. There is a brief primer in the beginning, but it comes with the understanding that you already know a little bit about the Ruby language. Now it's time to check out some of the patterns presented in this book.

What are patterns?

The book starts off with a chapter devoted to giving background to design patterns and how they can help you build better applications. This chapter isn't a history lesson. The main points of this chapter teach you about awareness. They teach you how to look at your applications, and when and where patterns could best be applied. He gives what he calls Patterns for Patterns while developing:

  • Separate out the things that change from the things that stay the same.
  • Program to an interface, not an implementation.
  • Prefer composition over inheritance.
  • Delegate, delegate, delegate.
  • You ain't gonna need it.

These principles provide us the glasses with which to look through as we assess our code. You will want to keep these in the back of your mind as you read through the rest of the book.

What is Ruby?

As I stated in the beginning, this book isn't about teaching you Ruby. However, the author does take the second chapter to walk you through the basics. This chapter simply shows you a bit of the syntax, types, and constructs. This intro is very brief, but probably enough to provide a reference throughout the rest of the book if you haven't worked with Ruby before. With this primer into Ruby, the author wraps up the first part of the book and we are now ready to move on to the 14 patterns covered in this book.

The Patterns

Spotting patterns might not always be easy, even with the guidelines given in the first chapter. One of the strengths of this book is that each chapter poses a different problem and then tackles is using the suggested pattern. This allows you to see it in action, and allows you to get the wheels spinning on how the pattern is used and where else it could be used. After walking through each of the patterns, we are presented with a nice wrap-up of the pattern.

This book is not very long in length, so I won't go into great detail with each of the patterns, as they are covered very well in each chapter. You need the context to understand how all of the pieces fit together. So, without providing too much information, here is a list of the patterns covered in the book:

  1. Template method
  2. Strategy
  3. Observer
  4. Composite
  5. Iterator
  6. Command
  7. Adapter
  8. Proxy
  9. Decorator
  10. Singleton
  11. Factory
  12. Abstract Factory
  13. Builder
  14. Interpreter

If you are like me, then you will have read these chapters and be left with a desire to instantly start researching, practicing, and applying what you have learned. But we aren't done quite yet, there is still one more section to go. We now know what the patterns look like. We have seen the examples done solely in Ruby. Everything makes sense. But can we do more? Now we shift gears to look at patterns found in the Ruby language itself.

The Patterns + Ruby

The Ruby language itself has several patterns that make programming Ruby fun and elegant. The last part of this book shows us a few of those patterns, as they can be used extensively while building your application patterns. If you have ever browsed through the Ruby on Rails source code, then you will have seen all of the patterns listed above, as well as the patterns in Ruby. Here are a few of those:

  • Domain-Specific Languages
  • Meta-programming
  • Convention over configuration

Ruby is such an elegant language to work with, and it is due in many parts to these patterns found within the language. These aren't patterns solely used by Ruby, but the English-like nature of Ruby makes it fun to both read and write with these patterns.

Parting Thoughts

This book fits nicely with the Addison Wesley Professional Ruby Series, but doesn't require you to read all other books in the series to fully comprehend. While the progression of this book was done very well, starting you off with a discussion of patterns and Ruby, then diving right into the patterns, I did find myself reading this book several times. I didn't read it several times because I didn't understand it, but simply to read with a new outlook on how and where these patterns could be applied, or are currently applied out in the wild. I find this book not only to be very informative, but also an excellent reference as I look to solve different problems. If you are a programmer, you should definitely read Design Patterns. If you are a Ruby programmer, you should then read Design Patterns in Ruby.