“Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.”

Design Patterns: Elements of Reusable Object-Oriented Software

Imagine the time before fancy and automatic GUIs, and the case of a simple textview. Sometimes, we enter more information in it than it can show. The behavior which we are accustomed to in these cases is that suddenly a scrollbar will appear out of nowhere, and magic! we can scroll down to see the rest of the text entered. But do we always need that scrollbar? If we’d create a subclass for textview with the name TextViewWithScrollBar, and use it anyway, would it make sense?

No it wouldn’t, we’d only need those bars whenever we exceed the space given us within the textview. And we cannot see that fore. So what is the solution?

From the title, you can guess easily that it’s nothing else but the Decorator pattern. Notice the following: “Attach additional responsibilities to an object dynamically.”. Not a class, but an instance of a given class, the object. This is the key phrase here. But how can we achieve such behavior. The answer is simple, we need four players here:

  • Component: a very brief and lightweight interface for your objects that can have dynamic responsibilities (such as render a scrollbar).
  • ConcreteComponent: the object itself which implements Component.
  • Decorator: implements Component, and maintains a reference to a ConcreteComponent.
  • ConcreteDecotator: adds the dynamic responsibilities to ConcreteComponent, implements Decorator.

Now a code example for the above:

Continue reading



“Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.”

Design Patterns: Elements of Reusable Object-Oriented Software

A Composite is something really simple and straightforward, with countless examples in the computing world (at least I found quite many examples for it). You can easily imagine it as a tree structure: a node can be a composite, or a leaf. Composites can contain further composites and/or leaves, and so forth.

The big advantage in this pattern is that it lets you treat primitive and container objects in the same manner, thus simplifying your code. Now let’s understand the key players here:

  • Component: defines the interface for the objects of the composition, their default behavior, the method to get or set the child components.
  • Leaf: primitives of the composition which haven’t got child objects.
  • Composite: components that have child objects, defining their own child-management methods.
  • Client: works with composition through the component interface.

An example is the View object in the Cocoa Touch Framework (to get a bit further from the .NET Framework). Each View can have further Views which can be accessed through the parent View. But here’s a .NET sample:

Continue reading


“Decouple an abstraction from its implementation so that the two can vary independently.”

Design Patterns: Elements of Reusable Object-Oriented Software

The Bridge pattern is a bit mysterious for me, I’m trying to find a concrete implementation of it in the .NET Framework, but it is a bit hard task. The main point in bridge is to decouple the implementation from its abstraction, and let them evolve separately. We have four key players here:

  • Abstraction: is the interface our client is aware of. It also maintains a reference on the implementation (something moved into the RefinedAbstraction commonly).
  • RefinedAbstraction: implements Abstraction, and extends its functionality (in all examples, the reference to Implementor was moved here).
  • Implementor: defines the functionality of abstraction, and the methods to call.
  • ConcreteImplementor: extends implementor.

Now let’s see a real example:
Continue reading


„Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”

Design Patterns: Elements of Reusable Object-Oriented Software

As you may have noticed, with Singleton, we reached the end of the creational patterns. From now on, we’ll consider the benefits of each so-called structural pattern. You don’t have to be a genius to find out what structural patterns are involved in: defining the composition of larger structures, built from objects.

The first one in the list is called Adapter (you may (and I have) known it as Wrapper). It comes handy when you are dealing with incompatible types (to be strict, interfaces). There are four players in the adapter pattern:

  • Target: that is, what the client can use, the shape we need to from the adaptee.
  • Client: our code which works with the Target.
  • Adaptee: an existing interface, that the client needs to use
  • Adapter: the class which acts as a wrapper, and adapts Adaptee to Target.

Maybe I haven’t described the problem in the best available way, so let’s see a code sample for this one:
Continue reading