The longer I write programs the more I realize that the same problems keep showing up over and over again. For example, in a spreadsheet application when one cell is updated often other cells need to be notified of the change. Similarly, in a social network application when someone adds a post their friends need to be notified. Both of these problems are the same, "how does one notify other objects when one is changed?"
A design pattern is a solution to a common problem. These patterns are collected by the software development community to capture their experiences so that they can be shared with others. Knowing the most common design patterns will make you a better OO programmer.
A design pattern is not code. In the example above, the spreadsheet app and the social network app share a problem, and it turns out that they share a solution (see the Observer pattern below), but it is not likely that they will share any code. A design pattern might be used in 100 different programs but share none of the same code.
This post describes some of the most common object-oriented design patterns and it includes fully worked out programs that implement them. You will be asked to follow along with the programs below. I am using Java but I believe you can understand the code even if you use a different OO language.
Just click on the links and a code playback page will load (you might want to open each one in a new tab). Then click on the comments on the left hand side of the playback screen or hit the play button to drive the development of the code. You can download the code at any point and run it on your machine. There are some controls in the top right hand side of the screen to make the text bigger or to switch to blog mode (which is good for small screens).
Strategy
The Strategy pattern describes how to plug in different algorithms to a class while the program is running.
Singleton
The singleton pattern describes how to ensure that only a single instance of a class is created.
Composite
The Composite pattern describes how to compose whole/part relationships between similar elements.
Decorator
The Decorator pattern describes how to add new functionality to a class by wrapping it in another.
State
The State pattern describes how to react to a set of events depending on the state of the system.
Observer
The Observer pattern describes how to notify a group of objects when one changes.
Proxy
The Proxy pattern describes how to add functionality in between objects that have a client/server relationship.
Factory
The Factory pattern describes how to create families of objects together.
Visitor
The Visitor pattern describes how to add functionality to a hierarchy of classes without changing their interface.
Comments and Feedback
You can find all of these code playbacks in my free 'book', OO Design Patterns with Java. I am always looking for feedback so please feel free to comment here or to send me a message. You can follow me on twitter @markm208.
Top comments (0)