The course is part of this learning path
This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.
- Understand what design patterns are
- Learn about the myriad of design patterns that you can use in your projects
This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.
Basic knowledge of Java programming.
Hello dear friends. In this video, we will examine the Memento Design Pattern. Let's begin. As your application is progressing, you may want to save checkpoints in your application and restore back to those checkpoints later. The Memento design pattern described by the gang of four in their book is a behavioral design pattern. The Memento design pattern offers a solution to implement undoable actions. We can do this by saving the state of an object at a given instant and restoring it if the actions performed since need to be undone. Memento pattern is used to restore the state of an object to a previous state. The memento object should expose as little information as possible to the caretaker. This is to ensure that we don't expose the internal state of the originator to the outside world, as it would break encapsulation principles.
However, the originator should access enough information in order to restore to the original state. The memento design pattern is one of the 23 well known GOF design patterns that provide the ability to restore an object to its previous state. The Memento design pattern is implemented with the help of three objects; the originator, a caretaker, and a memento. The Memento pattern is a software design pattern that exposes the private internal state of an object. One example of how this can be used is to restore an object to its previous state, undo via rollback. Another is versioning, another is custom serialization. The Memento pattern is implemented with three objects; the originator, a caretaker, and a memento. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do.
To roll back to the state before the operations, it returns the Memento object to the originator. The Memento object itself is an opaque object. When using this pattern, care should be taken if the originator may change other objects or resources. The memento pattern operates on a single object. Let's suppose you are going to an event like a birthday party or marriage, you will buy some gift for that person. If that person sees that gift after five years it could remind them of your presence at that event. So, the gift is acting as a memento. Let's consider the example of a bank. If your bank debit some account mistakenly, you would call your bank and raise a query. The bank checks your transaction, and if the operation was done by mistake the bank would return you the debited amount. Here, the bank is rolling back your transaction by changing the state to the previous state.
As you see in this UML diagram, the memento pattern has three participants. Originator is the object whose internal state we like to store. The originator object creates a memento object to store in its internal state. So, the originator object knows how to save and restore itself. Originator is the object which gets and sets the values of memento objects. Caretaker is the object which knows why and when the originator needs to save and restore itself. The object operates on the originator while having the possibility to roll back. It maintains the history of the memento objects created. The caretaker takes a snapshot of originator before operating. Memento is the POJO object that contains basic state storage and retrieval capabilities. The memento object is immutable in general. The object holds the internal state of the originator and allows it to restore in. Memento preserves encapsulation boundaries and it simplifies the originator. I think that's the most important advantage of Memento.
Now I think we will understand better with a little example. Let's create a new project and name it memento. First create an originator class that will use Memento object to restore its previous state. In this we need a state parameter. Let's add getter and setter for state. Now we need two methods, also to save state in memento and get state of memento. Now create a memento class that will store internal state of the original object. Add state parameter and generate setter and getter. Now we will create a caretaker class that will be responsible for keeping the memento. Name will be caretaker. Create a list instance from memento. Now we need to add and get methods. Done. Structure is ready, let's use in main. First, create an instance from originator, create another instance from caretaker. First, we will set state to originator. Now we will save to Memento. SetState2.
Now save memento again. Let's copy for state three. And now we will set state four. Let's show the state changes to the users. Current state, first saved, second, and third. Let's try and look the result. As you can see, current state is state four. First saved is one, second is two and this is three. We have now completed the memento design pattern. I hope you have a better understanding of how the Memento pattern works. We'll continue with the Observer design pattern in the next video. See you in the following video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.