Memento Design Pattern In Java

Memento Design Pattern In Java

Today, I would like to discuss another behavioral design pattern called the Memento Design Pattern which is used to restore the state of an object to a previous state.

Today, I would like to discuss another behavioral design pattern called the Memento Design Patternwhich is used to restore the state of an object to a previous state.

Memento Design Pattern

  • The Memento Design Pattern is one of the twenty-three 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
  • Originator β€” The object whose internal state we like to store. The *Originator *object creates a memento object to store its internal state. So, the *Originator *object knows how to save and restore itself. The object which gets and sets the values of *Memento *objects.
  • Caretaker β€” 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 rollback. It maintains the history of the *Memento *objects created. The caretaker takes a snapshot of *Originator *before operating.
  • Memento β€” 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 it.
  • The classic example of the Memento Pattern is_ a pseudorandom number generator_ or _finite state machine_.
  • Git stashing is another example of the Momento Design Pattern.
  • The internal state of the *Originator *object should be saved externally so that the object can be restored to this state later. Also, the object's encapsulation must not be violated.
  • The caretaker requests a *Momento *from *Originator *before operating. And use that *Momento *to restore the *Originator *to its previous state if needed.
  • We can make Memento Design Pattern implementation more generic by using Serialization; that will eliminate the requirement of every class having its own *Momento *class .
  • The Memento Design Pattern can also be used with the Command Design Pattern for achieving _undo _of the commands.

mementodesign

To understand the Memento Design Pattern let's take the example of the *Employee *class storing and retrieving its state using *EmployeeMemento *class.

java tutorial design pattern momento momento design pattern

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Proxy Design Pattern in JAVA

In this blog, we will discuss Proxy Design Pattern, its example, and how it is different from the other design patterns. But, it’s important to have an understanding of the design patterns first. You can refer here.

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Design Patterns for Beginners - Design Patterns in Java

"Java Design Patterns for Beginners - Design Patterns in Java - Design Patterns Tutorial" will provide you with detailed knowledge about Java Design Patterns and along with it. Why do we need Design Patterns? Structure of Design Patterns. Types of Design Patterns. Creational Design Patterns. Factory Design Pattern. Overview of Design Patterns

Composite Design Pattern in Java

This post looks at how to use the composite design pattern in Java, specifically the objects used in the pattern: component, composite, leaf, and client.

Visitor Design Pattern In Java

The Visitor design pattern lets us separate algorithms from the objects on which they operate. See how to implement it to follow the open/closed principle.