# Memento

In 
Published 2022-12-03

This tutorial explains to you the design pattern named Memento (which is a Structural Pattern).

# Memento Pattern - theory

Memento Design Pattern in Java is used when we want to save an object's internal state so that objects can be restored to this state later. The Memento pattern is also known as Token.

Take a look at the following UML diagram representing the Memento design pattern (for my example):

In this example you can see how an employee (object) change an attribute (a name) and after that the hole object is restored using the Memento design pattern.

# Memento Pattern - example

Here is an example of using the Memento Design Pattern in Java:

package memento.java.pattern.example;
 
public class Employee {
 
    int id;
    String name;
     
    public Employee(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
     
    public MementoEmployee createMemento() {
        return new MementoEmployee(this);
    }
 
    public void restoreToMemento(MementoEmployee memento) {
        this.id = memento.getId();
        this.name = memento.getName();
    }
     
    public void printEmp(){
        String string1 = "ID= "+this.id+ " ; Name = "+this.name;
        System.out.println(string1);
    }
}
package memento.java.pattern.example;
 
public class MementoEmployee {
 
    int id;
    String name;
     
    public MementoEmployee(Employee employee) {
        this.id = employee.id;
        this.name = employee.name;
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}
package memento.java.pattern.example;
 
import java.util.ArrayList;
import java.util.List;
 
public class CareTaker {
    private List<MementoEmployee> mementoEmployeeList = new ArrayList<MementoEmployee>();
 
    public void add(MementoEmployee emp){
        mementoEmployeeList.add(emp);
    }
 
    public MementoEmployee get(int index){
       return mementoEmployeeList.get(index);
    }
}
package memento.java.pattern.example;
 
public class MementoJavaPatternExample {
 
    public static void main(String[] args) {
         
    CareTaker careTaker = new CareTaker();
         
    Employee emp =new Employee(10, "Smith John");
    emp.printEmp();
    careTaker.add(emp.createMemento());
     
    emp.setName("Emmanuil Brown");
    emp.printEmp();
    careTaker.add(emp.createMemento());
     
    emp.restoreToMemento(careTaker.get(0));
    System.out.println("\nAfter Restore:");
    emp.printEmp();
    
    }
}

When you run this example you will receive the following result: