Thursday, September 17, 2009

Gang of Four Design Patterns review


Design Patterns: Elements of Reusable Object-Oriented Software was the first published book to identify patterns in object-oriented programmind and has become a classic in the last ten years.

What is a design pattern?
In computer science, but also in architecture, a design pattern is a standard solution to a common problem. In the software engineering field, patterns fill the lack of functionality of an object-oriented language.
Whenever you hear the words Factory, Lazy Loading, Singleton or Iterator, the argument is design patterns:
  • tipically the name of a design pattern is written capitalized: Iterator is one of them, while an iterator is a specific object;
  • design patterns are built with classes and objects and their representation is often a Uml diagram. When a set of classes you write falls in the diagram definition, it's said that you are implementing the pattern.
  • a pattern is a reusable technique and does not consist in reusable code: you may write many Factory classes but they will never be interchangeable. What is similar in them is the underlying concept and contract structure, the recurring theme.
  • also Anti-patterns exist, but they are not commonly teached.
This book, written by the Gang of Four and published in 1994, is the first presentation of patterns for the general developers public. It has sold more than 500.000 copies and even nowadaysl it does not have an alternative as a complete reference manual for the original patterns.
The name Gang of Four for the authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides is mutuated from the original Chinese Gang of Four, which was an alliance of (obviously four) communist party officials.

Chapter 1 (Introduction) and chapter 2 (A case study)
The beginning of this book already provides great value. The first chapter is about object-oriented programming, a recap of its principles, and a 50000 feet view of the patterns and how they fit together. Composition and inheritance are in the list of the topics; a guide for the reader helps to orient in the book, searching the right pattern for the job.
The second chapter is a study for the design of a WYSIWYG text editor a la OpenOffice, whose architecture is composed a bit at the time applying eight design patterns. It can be useful to read the practical solution of a real world problem and how Gang of Four manages to implementing the patterns presented.

After this introduction, the patterns are presented. Every pattern is described in the book as follows:
  • Name and classification: the name of this patterns have become a standard and they are referred all over the Internet. The classification will tell you what king of pattern you are viewing (creational, structural or behavioral).
  • Intent: every pattern solves a problem and it is stated in the most general form.
  • Also known as: other names for the pattern, now mostly in disuse.
  • Motivation: why the problem is a real, painful problem and how the pattern solves it brilliantly. This is the main explanation of how the solution works.
  • Applicability: typical cases where you will reach these pages of the book to read how to implement the pattern.
  • Structure: Uml class diagram or object diagram
  • Participants: descriptions of the classes involved in the diagram, with generic names.
  • Collaborations: the method calls of the objects.
  • Consequences: what this pattern application implies and its advantages or limitations.
  • Implementation: long notes on the best ways to implement in code this pattern.
  • Sample code: it speak by itself; a complete example of C code which shows you how to do practical work with this pattern. Although there is little mumbo jumbo in this book, I appreciate a lot this parts which keep out it and present something that compiles.
  • Known uses: object-oriented application which has applied this pattern successfully.
  • Related patterns: similar patterns to consult if the current solution feels wrong or raise other issues than the one it solves.
As you can see, nearly everything you want to know about writing an Iterator or a Builder is kept in this book.

Creational patterns (5 chapters)
This collection of patterns is dedicated to the creation of objects, solving problems like abstracting away a particular kind of objects behind an interface and simplify the creation process by eliminating repetitive code.
Abstract Factory, Builder, Factory Method, Prototype and Singleton are presented in this first big part.

Structural patterns (7 chapters)
This second collection presents patterns used for maintain a clean object graph: the typical intents are sharing interfaces and objects for reuse or adding responsibilities to subclasses without cluttering the class tree.
Adapter, Bridge, Composite, Decorator, Facade, Flyweight and Proxy are presented in this section.

Behavioral patterns (11 chapters)
These patterns are concerned with building a standard architecture for objects and with assigning responsibilities to the right owner. The intents are retain loos coupling between classes while at the same time allow complex algorithms and control flow to happen. For instance, abstracting way the State of an object to decouple it.
Chain Of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method and Visitor are presented here in this last part.

A glossary, a guide to the Uml notation and the foundation classes, on which the code examples are based, are also provided at the end of the book. This bonus will teach you also the basics of Uml.

In sum, this book is a milestone in the history of object-oriented programming and it will always have a reserved space next to my desk into my hard disk to allow a rapid consultation. I won't advise to read it all the first time, as it can be boring, but having it at hand can help whenever you're thinking if the design of your application can be improved.

The image at the top is the cover of the current edition of the book. There is also en electronic version: Design Patterns CD: Elements of Reusable Object-Oriented Software (Professional Computing).

4 comments:

Jim said...

I have a love/hate relationship with this book. I tried to read it on about three separate occasions, and I always lost steam around page 80, just as it’s getting in to the patterns. I eventually learned the patterns from resources on the internet, such as university lecture notes and design pattern enthusiasts. Once I understood most of the patterns, I went back to GoF, and I was able to read it from cover to cover.

I love that the GoF cataloged these OO techniques. However, I really dislike the organization. I feel they got a few patterns in the wrong category, for example, Flyweight is really a creation pattern. I’m also surprised they didn’t include Object Pool, which had to have been a well known technique at the time.

They also completely ignored the fact that objects obtained from the creation patterns need to be deleted, released, etc. The only way the user knows what to do with the object is to know details of how it was obtained, and this starts to break the encapsulation of the pattern.

Speaking of the user, the GoF don’t make it obvious which parts of the pattern are provided by a programmer, which parts are to be used by a client and whether another actor, such as the supplier of the breeder object for Prototype, is involved. Finally, they don’t suggest how many of the structural patterns (Decorator, Composite, Interpreter, et al.) are configured. In explaining these patterns to others, I had to define the Configurer myself and explain the responsibilities of this role.

Giorgio said...

Jim,
thank you for your feedback. I intended this book much as a reference one and not for a cover to cover reading, which can be very boring. Whenever I'm implementing a pattern of the classical series and I have doubts regarding my choice and its implementation, I know I can go back to the book to find the "standard" solution.
Obviously, once you know the standard, no one keeps you from working on variants.

Anonymous said...

I have bought a czech translation of GOF, but i have not read it because the translation was miserable, i stopped after 10 pages. I have downloaded the electronic version in english, but i find it boring too. My favorite book on patterns is Design Patterns by Christopher G. Lasater http://www.jbpub.com/catalog/9781598220315/ The examples are in C#, but i had no problem to rewrite them to PHP.

Jozef

Giorgio said...

The only way I can read this book cover-to-cover is by writing posts for all the patterns (which is what I'm doing), otherwise it is boring. But whe you have a doubt on a pattern, you can open the book to see all the possible variations. :)

ShareThis