
Episode 20 · Season 5
Software Design - The Memento Pattern
About this episode
We tackle the memento pattern in this episode. If you ever wanted to store the state of an object, then this is the one for you. You can think of this pattern as a way to take note of your instance and store it for later.
The Memento Pattern Defined
As always, we will start with the "Gang of Four" intent to set the stage for our discussion.
"Without violating encapsulating, capture and externalize an object's internal state so that the object can be restored to this state later."
The trick to this intent is to avoid violating encapsulation. The whole point of this pattern is to allow us to store and pass around the memento without having to keep track of the ways and places it is used. Thus, proper encapsulation will enable us to keep the memento object self-contained outside of the knowledge it requires of the classes that use it. Of course, that brings us to an essential part of this pattern. It can sometimes be used to store the state and context of several types of objects. Therefore, it does not have to be a one-to-one relationship. Even the caretaker classes can be used to handle a large number of memento types when correctly designed.
Applying The Pattern
In most cases, you will want to start with an interface for the memento. That will give you a signature that the caretaker object will use to handle the memento objects. Then the memento itself is going to implement that interface. You might even have a hierarchy of them to progressively store and transfer more details (properties) of the objects they work with. Therefore, you will find access level restrictions along the lines of a friend or package level tend to work best.
Java, PHP, C#, etc.
This is one of the few patterns that can be implemented very differently depending on the language you are working with. In particular, the languages that have a friend or helper concept built into them are going to have better-looking implementations. This improvement is because implementing this pattern falls into that category of relationship.
More from Develpreneur: Become a Better Developer and Entrepreneur
A handful of people love your MVP. Their suggestions keep coming, and AI helps you turn those requests into working features quickly. The product is growing. Whether it is growing in the right directi…
Your team can generate an interface faster than ever. Meanwhile, customers are still staring at confusing error messages, waiting for pages to load, and trying to figure out what happens after they cl…
After a year of deadlines, long hours, and projects that seemed determined to last forever, Thanksgiving offers a chance to pause and enjoy what all that effort has made possible. In this Thanksgiving…
The holiday is almost here, but your brain is still at work. There is something to finish, something to check, and probably something you could squeeze in before everyone arrives. Closing the laptop s…
Building a successful business requires more than developing a great product. As companies grow, they face new challenges involving customer expectations, operational efficiency, financial decisions,…