Design Patterns: Elements of Reusable Object-Oriented Software (Introduction)

(Article copied from my old blog)
Design Patterns: Elements of Reusable Object-Oriented Software

Design Patterns: Elements of Reusable Object-Oriented Software is the first and probably the most famous book about the Design pattern. It was first published in 1995, but thankfully those design patterns are still valid nowadays, the only outdated thing in this book may be the Smalltalk programming language, used in some examples. Anyway this book is still very good and popular: in fact I have decided to read this book when, in a job listing, it was expressly requested the knowledge of design patterns and of this book!

I will write a series of 15 articles about this book, making the summary of a couple chapters of this book in each one (the book is pretty big, it has over 500 pages). To deepen your knowledge of the argument, you can always buy the book @ Amazon.

The design patterns

The design patterns were born the moment in which some good software architects realised that making the perfect design at the first attempt was pretty difficult if not impossible at all.

The perfect design was supposed to be as flessible and as reusable as possible, unfortunately a such design was (almost) obtained after at least a couple of refactorization cycles: it was crucial to be abele to reuse good solution, what have worked in the past for the new projects.

The design patterns are exactly this: they are “previous good solutions, applicable to a family of problems and situations with some traits in common”.
Four computer scientist collected the most used design patterns (as far as I know they did not really invented them), in a comprehensive book, that had become really popular in the subsequent years. Those computer scientist are now know as the Gang of Four or GoF, even if they do not even know exactly the reason, and the book is commonly referred to as “The Gang of Four Design Patterns”.

Are all the design patterns similar?

No. In fact they are divided by the Gang of Four in three groups, with different purposes. These groups are Creational patterns, Structural patterns and Behavioral patterns.

Creational patterns

They are related to the creation of objects. Each one amongst the pattern will be somewhen introduced in one of the articles of this series, now I will just list them:

Factory method, Abstract Factory, Builder, Prototype, Singleton

Structural patterns

The structural patterns are related to the composition of classes or objects. They are:

Adapter, Bridge, COmposite, Decorator, Faced, Flyweight, Proxy

Behavioral patterns

Those patterns regulate the interation between classes or objects. They are:

Interpreter, Templated Method, Chain of Reponsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Visitor

Principles behind the design patterns

The key is to program to an interface, not an implementation, trying whenever is possible to do not declare variables to be instances of particular concrete class. In this way the client remain unaware of the specific types of objects they use, they just refer to the interface.

Another key principle is favor object composition over class inheritance, because the latter breaks encapsulation and can not be defined dynamically at run time, with dynamic references to other objects.

The delegation is a way of empowering composition as far as concern the reusability. In delegation, each request to an object called is forwarded to another object, the delegate.

In the next article of this series, we will introduce the first Design Patterns, Composition and Strategy.

If you want further information about the introduction to design pattern I have summarized in this article, I suggest you to purchase the GoF book in the Amazon online store.