Memento Design Pattern has a lot of applicability when it comes to restoring the state of an object. It is a part of the behavioral design model that concerns algorithms and the assignment of responsibilities between objects. The intention of this design pattern is not to violate the principle of encapsulation by not exposing information outside of the desired objects. In this article, we will discuss scenarios in which we can use the memento design template, talk about its pros and cons as well as its advantages. Let’s start with an example.
Marketing Analyst at Decipher Zone Technologies Pvt. Ltd.
The flag design template is a software design template that is used to revert an object to its previous state. It is a part of the behavioral design model that concerns algorithms and the assignment of responsibilities between objects.
Behavioral models describe models that deal with communication between objects or classes. Memento Design Pattern has a lot of applicability when it comes to restoring the state of an object. Applications that require users to revert to a previous state use the flag design template.
The intention of this design pattern is not to violate the principle of encapsulation by not exposing information outside of the desired objects. In this article, we will discuss scenarios in which we can use the memento design template, describe what the memento design template is, talk about its advantages and disadvantages. Let’s start
What is the memento design template?
The intention of the flag design model is to externalize the internal state of an object so that the object can be restored to that state later without violating the principle of encapsulation. Gaming applications should keep snapshots of the levels the player has played. As the user progresses through different levels in a game, the chances of encountering an end game situation increase.
Most games allow users to restart the game from the previous level. Memento design template ideas are used in most circumstances when it comes to storing and restoring state in games.
Another example can be taken from a drawing application such as paint in the Microsoft Windows operating system or any other graphics editor that supports connecting objects. We can create two circles and join them using a line. Now, if we move the rectangle system by selecting the whole figure or by some other means, the rectangles should stay connected.
The graphics editor makes sure that the line stretches appropriately to maintain the connection between the rectangles. If we try to make our own graphic editor, we can use the memento design template to make sure that the desired effect is achieved. Such applications use an object that calculates length, position, and angle using mathematical equations.
This object uses the help of objects in the memento design model toolbox to store the previous constraints (length, position, angle). So if the user moves the rectangular system the next time, the previous state can be used to determine the next position.
The flag design template consists of an object that stores a snapshot of the internal state of another object, which is the author of the flag. The cancellation mechanism requests a flag from the sender when it needs the list of sender status checkpoints. The sender is the one who initializes the flag with information that characterizes its current state.
The restriction is that the author is the only one who stores and retrieves the information from the memento. Let’s take a look at these participants one by one to fully understand what it all means –
The memento object stores the internal state of the Originator object.
The sender has access to the flag object and no other object directly interacts with it.
The sender himself is responsible for creating a flag that contains the snapshot of his current internal state.
In addition, it uses the flag to restore the internal state
The guardian class is responsible for saving the snapshot of flag states. It maintains a list of states which is then used by the sender to return. You will understand the use of this class using a sample memento design template.
Sample memento design template
The memento class consists of the state variable, in our case we used a simple string to represent the state. The Memento class has two methods; one is the constructor and returns the current state.
Fig: Example of a memento design template in java
Memento keeps a single snapshot which is then maintained as a list by the custodian object
The sender is responsible for setting the state of the object and it uses the setState () method to do so. The saveStateToMemento method returns a new Memento object with the current state stored in it which is stored in the memory list of the CareTaker object.
And getStateFromMemento returns the current snapshot of the state.
Fig: example of a memento design model in java – The author is at the origin of the report
The guardian class maintains a list of state snapshots in the form of flag objects. It is from the guardian object that we can jump into the different states that we have stored because it is the one that contains the complete list. The get method returns the snapshot using the index passed to it.
Fig: Sample java memento design template – CareTaker maintains a status list
4. Souvenir model demo
As you can see, the initiator object sets the state and the snapshots of the memory object are added to the list managed by the custodian object. In order to get the current level, we can use the getState () method directly from the original object. However, the guardian object is the one from which we can get the snapshots of the state and reflect it on the original class.
The getStateFromMemento () method gets the “level” (state) we want to jump into and sets it to the state variable of the Originator object. Now if we use the getState () method directly from it, we can see that we have restored a previously saved state from the list maintained by the guardian object.
Fig: Example of a memento design template in java – Memento template in action
What are the pros and cons of using the memento design template?
- Memento preserves the limits of encapsulation by avoiding exposing information that only a sender should handle. However, this should be stored outside of the sender, as we saw in our sample code. We have stored this in the guardian object.
- The memento design model allows us to keep our code simple on the client side by letting the custodian manage the list of states.
- Keeping the recorded state outside of the key object helps maintain cohesion.
- It provides an easy way to implement checkback / rollback functionality for an application.
- Tags can use a lot of memory if the author has to copy a large amount of information to be stored in the tag.
The Memento design model has two purposes:
- One is to save the important state of a system’s key object.
- The other is to maintain the encapsulation of the key object.
Read: Factory Design Patterns in Java
The Memento design model promotes the principle of single responsibility by providing an option to separate the state we are saving from the key object. And this separate object, as we have seen, is the memory object. However, the list of state snapshots is maintained by the guardian object and not by the flag.
Create your free account to unlock your personalized reading experience.