Design Patterns Tutorial
Design Patterns Resources
Selected Reading
- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Patterns - Memento Pattern
Design Patterns - Memento Pattern
Memento pattern is used to restore state of an object to a previous state. Memento pattern falls under behavioral pattern category.
Implementation
Memento pattern uses three actor classes. Memento contains state of an object to be restored. Originator creates and stores states in Memento objects and Caretaker object is responsible to restore object state from Memento. We have created classes Memento, Originator and CareTaker.
MementoPatternDemo, our demo class, will use CareTaker and Originator objects to show restoration of object states.
Step 1
Create Memento class.
Memento.java
pubpc class Memento { private String state; pubpc Memento(String state){ this.state = state; } pubpc String getState(){ return state; } }
Step 2
Create Originator class
Originator.java
pubpc class Originator { private String state; pubpc void setState(String state){ this.state = state; } pubpc String getState(){ return state; } pubpc Memento saveStateToMemento(){ return new Memento(state); } pubpc void getStateFromMemento(Memento memento){ state = memento.getState(); } }
Step 3
Create CareTaker class
CareTaker.java
import java.util.ArrayList; import java.util.List; pubpc class CareTaker { private List<Memento> mementoList = new ArrayList<Memento>(); pubpc void add(Memento state){ mementoList.add(state); } pubpc Memento get(int index){ return mementoList.get(index); } }
Step 4
Use CareTaker and Originator objects.
MementoPatternDemo.java
pubpc class MementoPatternDemo { pubpc static void main(String[] args) { Originator originator = new Originator(); CareTaker careTaker = new CareTaker(); originator.setState("State #1"); originator.setState("State #2"); careTaker.add(originator.saveStateToMemento()); originator.setState("State #3"); careTaker.add(originator.saveStateToMemento()); originator.setState("State #4"); System.out.println("Current State: " + originator.getState()); originator.getStateFromMemento(careTaker.get(0)); System.out.println("First saved State: " + originator.getState()); originator.getStateFromMemento(careTaker.get(1)); System.out.println("Second saved State: " + originator.getState()); } }
Step 5
Verify the output.
Current State: State #4 First saved State: State #2 Second saved State: State #3Advertisements