| Pattern name | Source code | Comments |
| Abstract factory | abstract_factory.cpp | An abstract factory is used to create objects. However you want to change what object is created. |
| Adapter | adapter.cpp | Convert one class so that it appears to be another class. |
| Bridge | bridge.cpp | Bridge allows two implementations to vary independently. In this case, the implementations of message and recipient are independent, but combine to output "Hello world!" |
| Builder | builder.cpp | Builder is a class (or set of classes) responsible for constructing an object. Each builder constructs a different part of the object. |
| Chain of responsibility | chain_of_responsibility.cpp | Performs staged processing using a series of different objects. |
| Command | command.cpp | Command contains an action that you need to defer for some reason. We split the task into two commands (PrintHello and PrintWorld), and queue them up in Commands. |
| Composite | composite.cpp | Composite combines several classes into a single class. |
| Decorator | decorator.cpp | Decorator adds or overrides functionality of a class. Unlike normal inheritance, decorator overrides behaviour of a derived class, not a base class. |
| Façade | facade.cpp | Façade hides a lot of complex behaviour behind a simple, single interface. It puts a shop-front on an otherwise complex set of classes. |
| Factory method | factory_method.cpp | Factory method creates some object (in this case a string) in a derived class, for use in a base class. |
| Flyweight | flyweight.cpp | Flyweight creates objects as they are needed, because keeping them around is unnecessary. Here the Character class is a flyweight that is only used for the duration of the inner loop. |
| Interpreter | interpreter.cpp | Interpreter processes commands and performs actions based on its input. Here we create a simple command language consisting of single characters. |
| Iterator | iterator.cpp | Iterator provides a place-holder into a collection. In this example, StringIterator provides a placeholder into a string. Note that C++ iterators are similar but have a different interface. |
| Mediator | mediator.cpp | Mediator binds different classes together, and the individual classes communicate via the mediator, never directly. In this example, Message::print() calls Mediator, not Printer directly. |
| Memento | memento.cpp | Memento stores a previous state or value so that it can be restored at a later time. |
| Null object | null_object.cpp | Null object creates a special object to mean nothing/null/absent/default. It means that the default behaviour can be implementer in the null object instead of making an explicit check for null, or using NULL pointers. |
| Observer | observer.cpp | Observer is used to watch events on another object. Here, the Writer class keeps an eye on the MessageBoard class, and writes the message each time MessageBoard::post() is called. |
| Prototype | prototype.cpp | A Prototype is an object which is cloneable, i.e. you can create a copy, even though you don't know what you are creating a copy of. |
| Proxy | proxy.cpp | Proxy allows one object to stand for something else. So whilst you think you are dealing with one object, you are actually dealing with something else. In this example, CoutProxy stands for std::cout. |
| Singleton | singleton.cpp | Singleton ensures that there is one, and only one instance of a class. Like global variables, everybody has access to that instance. Singleton should be used sparingly since the assumption that there should be just one of something usually ends up being wrong. Singleton is hard to write unit tests with. |
| State | state.cpp | State is like an enum, but we can define different behaviour for each state. We could add another class, Sad, which did something entirely different when asked to talk(). |
| Strategy | strategy.cpp | Strategy is an algorithm that you pass into a function or class that uses the strategy. The idea is that you can provide a different strategy to achieve something different. |
| Template method | template_method.cpp | Template method provides an implementation in a derived class, to be used by the base class. |
| Visitor | visitor1.cpp, visitor2.cpp | Visitor provides a call-back for every item in a collection. This
allows the implementation of the collection to hidden. visitor1.cpp is the simplest type of visitor using templates. The std::for_each template function that takes any kind of type. visitor2.cpp uses the more traditional inheritance-based polymorphism. |
Conclusion
References
Exercises
A note on virtual functions
A note on references