A guide to software design patterns

Behavioural Design Pattern: Memento

Petey
Petey
Feb 13 · 5 min read
Photo by Joshua Rawson-Harris on Unsplash

The Memento pattern is one of the eleven behavioural design patterns. Memento pattern is used to restore an object to its previous state. It is also known as a snapshot pattern. This is simply a way of tracking states of an object and having the ability to revert previous changes.

Implementing this pattern requires three objects:

  • originator
  • memento
  • caretaker

The originator is the object we’ll be making changes to. It provides a way to create and restore mementos.

The memento is a snapshot of the originator at some point in time. Mementos can only be created and read by the originator and cannot be changed once created — i.e. mementos are immutable.

The caretaker is the object that performs “create” and “restore” memento operations on the originator object. Think of the caretaker as an administrator that is capable of taking snapshots (mementos) of a system (originator) and restores those snapshots at will.

Pattern Ideal Usage

When would this pattern be a good solution? Let’s say you’re designing a real estate system that must provide agents with the ability to change and restore a property’s price. Ideally, every time a price change happened, we’d create a memento to be able to revert. We need to create three objects:

  1. PropertyMemento — memento
  2. Property — originator
  3. Agent — caretaker

Both PropertyMemento and Property classes will have getters, but we’ll only have setters in the Property class to make sure the PropertyMemento is immutable. For brevity, we won’t be doing any validation, we’ll just assume we’ll always get the right values.

Here’s the code for the PropertyMemento class:

Here’s the code for the Property class:

I’ve overridden toString() method to print a format of my liking, you can change it according to whatever object you’re working with.

The Agent class will have a list of all create mementos methods to set the price of the property, create, restore, and get all created mementos.

Here’s the code for the Agent class:

We can use the Agent class in your main method to simulate an agent behaviour like this.

Your output window now looks like this:

It’s important to point out that the date value is the same for all the created mementos because I’m using a fast machine. You can delay the execution to see different times in the mementos.

That is pretty much the memento pattern in a nutshell. Thank you for making it to the end.

Happy coding.

The Startup

Medium's largest active publication, followed by +582K people. Follow to join our community.

Petey

Written by

Petey

Husband, father, engineer, musician, and writer by luck. I write mostly about whatever comes to mind. Follow me on this crazy writing journey if you dare.

The Startup

Medium's largest active publication, followed by +582K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade