Monday, June 22, 2009

Book Review – Head First Design Patterns

I really enjoy the Head First style of writing because it uses many subtle and some not so subtle distractions to keep the reader interested. So after reading Head First Object Oriented Analysis and Design I moved on to Head First Design Patterns. If you have never read a Head First book before you can get a feel for the style by reading this sample chapter.

The thing I liked most about this book was how I could apply the design patterns to code and tools that I had written in the past. I was one of those unfortunate souls who got my education through one of those schools Joel Splosky refers to a JavaSchools. We did not do a lot of work studying programming patterns. I recognized many of the techniques but I did not know the terms. This is expressed quite well in chapter one:

Design Patterns give you a shared vocabulary with other developers. Once you've got the vocabulary you can more easily communicate with other developers and inspire those who don't know patterns to start learning them.

HF_Guru One of my favorite attention grabbing techniques they used throughout the book was the Design Patterns Guru ( as seen on the right ). These sections were used as a way to summarize the points made throughout the chapter. As you progress through the book you move from a grasshopper to a worthy design patterns student. This is one of many techniques that keep the book moving forward while reinforcing the content.

Head First Design Patterns covers fifteen patterns in depth and highlights several more. In fact, entire chapters are devoted to a single pattern or two at the most. This combined with very good examples, written in java, make learning these patterns very easy to learn.

Some of the patterns I found the most interesting were; Factory, Observer, Decorator and Adaptor pattern. Factory patterns are used throughout .NET programming. In fact the Advantage .NET Data Provider contains a Factory class which is used to instantiate the various data objects. The Observer Pattern can be found in database Notifications. Decorators and Adapters are similar in the sense that they both modify how an existing class works. However, they are much different in purpose.

A decorator is used to add new behavior to an object. The book uses a coffee shop application as an example. Consider a cup of coffee to be the class that you want to decorate. Once the roast has been selected customers may want cream, sugar, mocha or any other add in. Each of these add ins modify the original coffee and can be used on any type of coffee. This makes it easy to add new decorators that can be applied to any coffee class. Without decorators each coffee class would have to be modified for each new add in.

The Adapter class wraps around an object allowing it to be used by another object. The book uses an international plug-in adapter as an example. When you travel to Europe from the US you need to use an adapter to plug in your devices, assuming they can handle the 220 v. All of our Advantage clients can be thought of as adapters which allow the application to talk to Advantage. The Advantage Delphi Components translate Advantage API calls into easy to use TDataSet components making programming easier. Similarly the Advantage .NET Data Provider uses Advantage API calls to generate the standard .NET data classes.

Chapter twelve covers compound patters, because a real world application will use more than one pattern to get the job done. This is where the (in)famous Model-View-Controller (MVC) pattern is introduced. This is an often misunderstood pattern probably because we are so used to keeping everything together and not really separating out the different functional pieces. With the MVC model each layer is abstracted leaving providing a true "do one thing" principal.

This pattern is very prevalent in database programming and can be applied at several levels. The database (or tables) can be thought of as the model we are working with. The database server is the controller which interacts with the model. The view is the application which displays the data to the users so they can interact with it. Of course, this is a very high level which could be broken down into smaller pieces. For example, the Object Relational Model (ORM) produces it's own model which uses a data provider as the controller and the application as a view. This allows the ORM model to be different than the physical database model providing more flexibility to the developers.

Be sure to read chapter thirteen for some practical real world advice as well as some additional reading. There is also an appendix which covers eight more patterns if you didn't get enough from the first thirteen chapters.

The bottom line: I thought this book was very informative, the information was presented in a fun way and reinforced constantly throughout the text. I learned a lot about patterns and I agree that having this common vocabulary will be very useful when working on collaborative projects with other developers. I would recommend adding this book to your bookshelf as a reference to use when designing your new applications or features.

No comments: