Memento Design Pattern

Damsak Bandara
Nerd For Tech
Published in
5 min readMay 25, 2021

Memento Pattern is a Design pattern that is used to restore the state of an object to a previous state without revealing the implementation details. This pattern falls under the category of Behavioral design patterns. Think about a situation where we develop an application with states. Now there is a requirement saying that at a particular point, we need to go back to the previous state. A Memento Design pattern can be used to achieve this functionality.

There are 3 main components of the Memento Design Pattern

  1. Originator: Particular object whose state needs to be maintained. It creates the memento.
  2. Memento: Object that is responsible for maintaining the state of the originator.
  3. Caretaker: Object Keeps Track of multiple mementos.

Implementation

The Originator is coupled with the Memento and the state of the caretaker. So whenever we need to go back to the previous state, we talk to the caretaker and get the previous state and move.

Let’s try to understand this pattern with the help of a real-world example.

Use Case

“The Central Hospital” is a famous private hospital located in Colombo. There are about 200 doctors who provide various treatments to their patients.

Requirement — The doctors need to keep track of the drugs issued to the patients.

Treatment continuation is based upon the state of the patient. If the patient is not doing well the doctors may introduce new drugs to the existing list. Or else if the patient is doing well, the doctor may reduce the intake.(Assume that these are done for patients with long-term conditions who need to take drugs on a regular basis. )

Analysis —

  • We need to keep track of the previous list of drugs issued with the dosage.
  • We need the functionality to revert the current drug list to a previous drug list.

So it's clear that we can use the Memento Design Pattern for this situation. Let’s implement a solution.

Step 01:

Let’s implement the Medicine class which represents a particular Medicine.

Step 02:

As mentioned in the previous section, the Memento Design pattern contains 3 main segments. Let’s implement the Originator and the Memento classes. The caretaker (PrescriptionHistory class) needs to maintain the state of this Memento class.

Import Points :

  • .clone() method inside the getter — If we just copy the list to another, then the modifications done to the second list effects the original list as well. That is because we copy the references. Therefore clone() should be used.
  • private getDrugs() — Only the Prescription class can access this method.

we implemented 2 important methods in this class —

  • .save() — Preserve the current state. Give the state to the caretaker(Prescription History) by giving the MedicineMemento. If we give the Prescription object itself we cannot change it without affecting previous states. That’s why we pass a MedicineMemento. Basically, a new MedicineMemento object is created using the current Prescription and pass it.
  • revert() — Revert the current state. Get the MedicineMemento itself and set the current state to the state from the MedicineMemento.

Step 03:

Now we need to implement the caretaker class. This class should hold the state of the previous object.

Behavior — When we revert, the last prescription list should be the one to come first. That means the behavior is Last In First Out. Therefore we can use a Stack.

Step 04:

Now we can implement the main application class. Let’s try to understand this will the use case of an actual patient.

  • A particular doctor assigns 2 drugs to be consumed to a patient.

Implementation —

Output —

Fig 1:1st prescription
  • The patient is still feeling sick after 1 week. Therefore doctor increases the treatment by introducing a new drug to the existing list. Let’s implement this scenario.

Implementation —

Output —

Fig 2: Updated Prescription

Now we can see that we have successfully added the new drug to the list.

  • The patient is feeling better after using the new drug for 1 week. The doctor decides to remove the newly added one and prescribe the old list.

Implementation —

Output —

Fig 3: Prescription after 2nd Update

Explanation

  • The application reverted 2 times Even though we executed the revert method 3 times. Why is that?

The execution does not depend on the items inside the Prescription. The first prescription contains 2 drugs and 2nd one contains 3 drugs. The 1st revert will remove the 2nd updated prescription. When we execute the revert method for the second time, the application says it can't do it because there’s only 1 prescription version.

  • The updated Prescription is printed twice. Why is that?

We basically add the new drug to the prescription and gave it to the prescriptionHistory. Now the caretaker(prescriptionHistory) will give the last prescription. It is equal to the current updated prescription. That is why we get the same prescription after reverting once.

How to solve this?

  • Avoid passing the newly added drug to the prescriptionHistory. This can be done by removing the following line from the implementation.

presHistory.save(prescription);

Updated Output —

Fig 4: Output with no repeats

Now we can see that there are no repeating prescriptions. So we have successfully implemented the above solution.

The following URL provides the complete source code for the above implementation.

I have used the following playlist by Mr.Krishntha Dinesh to gather the required information.

--

--