DEV Community

Cover image for Object-Oriented Design Patterns
Mark Mahoney
Mark Mahoney

Posted on • Updated on

Object-Oriented Design Patterns

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).


The Strategy pattern describes how to plug in different algorithms to a class while the program is running.


The singleton pattern describes how to ensure that only a single instance of a class is created.


The Composite pattern describes how to compose whole/part relationships between similar elements.


The Decorator pattern describes how to add new functionality to a class by wrapping it in another.


The State pattern describes how to react to a set of events depending on the state of the system.


The Observer pattern describes how to notify a group of objects when one changes.


The Proxy pattern describes how to add functionality in between objects that have a client/server relationship.


The Factory pattern describes how to create families of objects together.


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)