Thursday, August 23, 2012

Head First Design Patterns Book Review

Book cover from
I guess every software engineer should know about design patterns, they give us a way to solve design problems based on the experience of many other software engineers who have had the same or similar challenges in OO design. To be honest, I remember studying them during my B.S., applied some of them in my projects, but after years of experience in software development, is now that I realize how useful they may be. In this post I'm going to review the book "Head First Design Patterns", written by Eric Freeman, Elisabeth Robson, Bert Bates, Kathy Sierra and published by O'Reilly.
As others "Head First" titles, this book is well written, easy and funny to read, it has a lot of graphics, jokes, examples in the book and on the web site to be downloaded... I just loved it! Click here to know why Head First books are great.

The book covers the following design patterns:
  • Observer: Subscribe to receive updates when object's state changes.
  • Decorator: Wraps an object to provide new behavior.
  • Factory: When you want to encapsulate instantiation.
  • Singleton: Ensures one and only one of a kind.
  • Command: Encapsulates a request as an object.
  • Adapter: Wraps an object and provides a different interface to it.
  • Facade: When you need to simplify the interface of a set of classes.
  • Template Method: Subclasses decide how to implement steps in an algorithm.
  • Iterator: Allows to traverse a collection without exposing its implementation.
  • Composite: When you need to treat collections of objects and individual objects uniformly.
  • State: Encapsulates state-based behaviors and uses delegation to switch between behaviors.
  • Proxy: When you need to control access to an object.
  • Some other patterns: Bridge, Prototype, Interpreter, Chain of responsibility, mediator, etc.

Each pattern is explained in detail using an application sample in what they have called "ObjectVille". They also give you examples of its use in real life and its class diagram. Each chapter has a summary at the end (they've called it "Tools for your Design Toolbox") with the most important concepts and definitions.

Many of the above patterns were discovered by the Gang of Four (GoF), who were the first ones in writing about this topic back in 1995. Since then, new patterns have been discovered (yes, they are discovered not created), this book guides you to other reading materials where you can find more design patterns, shows you how looks the formal documentation of a pattern and describes the steps you should consider if you discover new ones.

But it is not all about design patterns, the book also explains the Object Oriented Design Principles (OODP) that every software engineer must know in order to design extensible and maintainable software:
  • Encapsulate what varies
  • Favor composition over inheritance
  • Program to interfaces, not to implementations
  • Strive for loosely coupled designs between objects that interact
  • Classes should be open for extension but closed for modification
  • Depend on abstractions. Do not depend on concrete classes
  • Only talk to your friends
  • Don't call us, we'll call you
  • A class should have only one reason to change

Something really important I learned with this book is: Always use the simplest solution that meets your needs, even if it doesn't include a pattern. So don't try to fill your design with patterns all around, first try using the Object Oriented Design Principles (OODP) then, if necessary, make use of patterns.

One more thing, the book is published in a lot of formats: printed book, kindle, PDF, so you have many options to read it. I have the kindle edition and it looks pretty much the same as in PDF. The kindle edition is available only on Kindle Fire, iPad, PC and Mac (not the portable kindle device).

In future posts I'll write about these patterns and how they are applied in the Java API.

See ya!!