Object-Oriented Design Patterns
Tags: designpatterns, tutorial, java, oop
Published: 2021-04-20 22:17:02

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.
* [Strategy Pattern: Adjusting Grades in a Course](https://markm208.github.io/patternbook/chapter1/01/index.html)

####Singleton
The singleton pattern describes how to ensure that only a single instance of a class is created.
* [Singleton Pattern: A Testable Random Number Class](https://markm208.github.io/patternbook/chapter1/02/index.html)

####Composite
The Composite pattern describes how to compose whole/part relationships between similar elements.
* [Composite Pattern: Displaying a Hierarchical File System](https://markm208.github.io/patternbook/chapter1/03/index.html)

####Decorator
The Decorator pattern describes how to add new functionality to a class by wrapping it in another.
* [Decorator Pattern: Logging with Decorators](https://markm208.github.io/patternbook/chapter1/04/index.html)

####State
The State pattern describes how to react to a set of events depending on the state of the system. 
* [State Pattern: String Splitting for Search Bars](https://markm208.github.io/patternbook/chapter1/05/index.html)

####Observer
The Observer pattern describes how to notify a group of objects when one changes.
* [Observer Pattern: Observing the Time Change](https://markm208.github.io/patternbook/chapter1/06/index.html)

####Proxy
The Proxy pattern describes how to add functionality in between objects that have a client/server relationship.
* [Proxy Pattern: Dealing Cards from a Remote Deck](https://markm208.github.io/patternbook/chapter1/07/index.html)

####Factory
The Factory pattern describes how to create families of objects together.
* [Factory Pattern: Getting Help in Mac and Windows](https://markm208.github.io/patternbook/chapter1/08/index.html)

####Visitor
The Visitor pattern describes how to add functionality to a hierarchy of classes without changing their interface.
* [Visitor Pattern: Adding Functionality to a Hierarchy of Classes (File and Directory)](https://markm208.github.io/patternbook/chapter1/09/index.html)

###Comments and Feedback
You can find all of these code playbacks in my free 'book', [OO Design Patterns with Java](https://markm208.github.io/patternbook/). 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](https://twitter.com/markm208).