Design Patterns: Learn to Love Them

As a programmer you’re going to have to solve similar problems over and over on different projects. Design patterns are techniques and solutions which allow you to solve common problems in elegant and reusable ways. Memorizing a few of the most common design patterns will make you a better problem solver. When an issue comes up that looks similar to one you’ve solved before you should be able to pull one of these solutions out of your back pocket and get the project done quickly and reliably. Another reason you should memorize design patterns is communication with other programmers. Since there are very common and widely used techniques for solving problems you can refer to a specific pattern in a tech spec or requirements document and other developers should know what you mean.

Here are a few from my back pocket:

Singleton (Creational)
An object which can only be instantiated once.

The purpose of a singleton object is property synchronization across the entire application. When the singleton is first initialized it stores a reference to itself and every subsequent initialization is referred to the existing instance.

An example use case would be a music playing application with a volume control that can be manipulated from multiple controls. The volume controller could be set up as a singleton in which case the keyboard event handler, plus and minus buttons and volume slider would try to initialize the volume controller object and change the volume property. Each method would act as though it controlled the only instance but in reality they would all be manipulating the same memory.

A singleton in C# looks like this:

class VolumeControl{
private static volumeControl instance;

// the constructor is protected to force
// a user to call the Initialize method
protected VolumeControl(){

public static VolumeControl Initialize(){
if (instance == null){
instance = new VolumeControl();

return instance;

Factory Method (Creational)
A method which uses a common base to initialize different types of objects.

The abstract factory method is overloaded to create and return different initialized types depending on the type of the current factory. For example I might create a method which generates track info objects for MP3s and WMAs. They both have different properties but are used in the same way.

A factory method in C# looks like this:

Adapter (Structural)
Allows a client to use a familiar interface to access the methods and properties of an object which would otherwise be incompatible.

Details to come…

Facade (Structural)
A single class that provides access to the properties and methods of many sub-classes.

Details to come…

Command (Behavioral)
An object which represents some message or action which can be passed between many other objects.

Details to come…

Observer (Behavioral)
An object which can be notified of changes to any object it observes and then carry out actions depending on the changes.

Details to come…