Memento Design Template Preview | Midi hacker

0

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.

picture
Profile picture of Mahipal Nehra Hacker Noon

Mahipal Nehra

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 –

1. Remembrance

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.

2. Author

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

3. Concierge

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

1. Remembrance

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.

picture

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

2. Author

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.

picture

Fig: example of a memento design model in java – The author is at the origin of the report

3. Concierge

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.

picture

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.

picture

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?

Advantages:

  1. 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.
  2. The memento design model allows us to keep our code simple on the client side by letting the custodian manage the list of states.
  3. Keeping the recorded state outside of the key object helps maintain cohesion.
  4. It provides an easy way to implement checkback / rollback functionality for an application.

The inconvenients:

  1. Tags can use a lot of memory if the author has to copy a large amount of information to be stored in the tag.
  2. Using the memento design template may not be a straightforward idea in most languages ​​such as JavaScript. It will be difficult to ensure that only the author can access the status of the memento.

In summary:

The Memento design model has two purposes:

  1. One is to save the important state of a system’s key object.
  2. 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.

Profile picture of Mahipal Nehra Hacker Noon

Key words

Join Hacker Midi

Create your free account to unlock your personalized reading experience.


Source link

Share.

Leave A Reply