Composite and Strategy from Design Patterns: Elements of Reusable Object-Oriented

(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 composite pattern

Like the name hints, this is a pattern designed to compose simple or already composed objects (without distinction between those two categories, by the adoption of a common interface) inside a tree structure.

There will not be differences between branches and leave regarding usage, the client will treat them uniformly: this is a really important fact, because it means the client will be simpler compared to a design with many have different objects with different interfaces and methods.

It is very useful in situations when we do not know in advance the level of complexity of the structure (through recursive composition we do not have to worry about the number of levels of composition).

The image from the GoF book was really complex, I think that the image you will find below, taken from a PDF called GoF Design Patterns – with examples using Java and UML2 released under a Creatuve cinnibs Attribution-ShareAlike 3.0 license, it’s more clear and easy to understand. composite_design_pattern

In the book there are several reccomandations about the implementation of the Composite Pattern:

The strategy pattern

coming soon

In the next article, I will introduce the Decorator pattern.

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.