3.8 메멘토 패턴 (Memento Pattern) .....행동 패턴(Behavioral Pattern) 중
메멘토 패턴은 객체의 상태 정보를 저장하고 사용자의 필요에 의하여 원하는 시점의 데이터를 복원 할 수 있는 패턴을 의미합니다.
■메멘토 패턴 예제 구조
▶ 실제로 메멘토 패턴을 사용하여 객체 정보를 저장하고 복원하는 예제를 살펴 보겠습니다. 예제는 간단히 String형 데이터 하나와 Int형 데이터 하나에 대한 정보로 가지는 Information 객체를 구현하였습니다.

● User : 메멘토 패턴이 적용 된 Information 객체를 실제로 사용하는 사용자입니다.
● Information : 상태를 저장하고 복원 할 데이터를 가지고 있는 클래스입니다.
● Memento : 특정 시점의 Information의 상태정보를 저장하는 클래스입니다.
● CareTaker : 상태 정보가 저장되어 있는 Memento들을 관리하는 클래스입니다. 내부에 Stack 자료형 변수를 가짐으로써 Memento 객체를 저장하고 복원을 합니다.
■ 구현
Information.java
package com.patterns.MementoPattern;
public class Information {
private String Data1; // Information이 가지고 있는 데이터1
private int Data2; // Information이 가지고 있는 데이터2
public Information(String Data1, int Data2) // 생성자입니다.
{
this.Data1 = Data1;
this.Data2 = Data2;
}
public Memento CreateMemento() // Memento를 생성합니다 (상태저장)
{
return new Memento(this.Data1, this.Data2);
}
public void RestorMemento(Memento memento) // Memento를 복원합니다 (상태복원)
{
this.Data1 = memento.getData1();
this.Data2 = memento.getData2();
}
public void set_Data1(String Data1) // 데이터1의 값을 지정
{
this.Data1 = Data1;
}
public void set_Data2(int Data2) // 데이터2의 값을 지정
{
this.Data2 = Data2;
}
public String get_Data1() // 데이터 1의 값 반환
{
return this.Data1;
}
public int get_Data2() // 데이터 2의 값 반환
{
return this.Data2;
}
}
▶실제 데이터 값을 가지고 있는 Information 클래스입니다. Information 클래스는 String형 데이터 Data1과 int형 데이터 Data2를 속성으로 가지고 각각 setter/getter 메서드가 구현되어 있습니다. 현재 Information 상태를 저장하기 위한 CreateMemento 메서드 ( 내부적으로 Data1과 Data2의 데이터 값을 가지는 Memento 객체를 생성)와 상태복원을 위한 RestorMemento가 구현되어 있습니다. RestoreMemento는 이전에 생성된 Memento 객체의 정보를 가져와 Information 상태 정보로 setting을 하게 됩니다.
Memento.java
package com.patterns.MementoPattern;
public class Memento {
//Information의 상태정보를 가지고 있음
private String Data1;
private int Data2;
public Memento(String Data1,int Data2)
{
this.Data1 = Data1;
this.Data2 = Data2;
}
public String getData1()
{
return this.Data1;
}
public int getData2()
{
return this.Data2;
}
}
▶Memento는 Information의 특정 시점에 대한 내부 상태 정보를 가지고 있습니다.
CareTaker.java
package com.patterns.MementoPattern;
import java.util.Stack;
public class CareTaker {
Stack<Memento> mementos = new Stack<>(); // Memento 관리를 위한 Stack
public void push(Memento memento) // 특정 시점에 생성된 Memento를 Push
{
mementos.push(memento);
}
public Memento pop() // 복원을 위한 Memento 객체 반환
{
return mementos.pop();
}
}
▶CareTaker는 Inforamtion의 상태 정보를 저장하고 있는 Memento들을 관리하기 위한 클래스입니다. 내부적으로 Memento를 저장하기 위한 Stack 자료 구조가 존재하며 push와 pop을 통해 Memento객체를 저장하고 복원을 하게 됩니다.
User.java
package com.patterns.MementoPattern;
public class User {
Information info;
CareTaker caretaker;
public void exe() {
info = new Information("Data1", 10); // Information 객체 생성
caretaker = new CareTaker(); // CareTaker 객체 생성
// 현재 Information의 상태 정보를 가지는 Memento를 생성하여 CareTaker에 추가합니다.
caretaker.push(info.CreateMemento());
// Information 정보를 수정합니다.
info.set_Data1("Data2");
info.set_Data2(20);
// 현재 Information의 상태정보를 출력합니다.
System.out.println("현재 Data1 : " + info.get_Data1());
System.out.println("현재 Data2 : " + info.get_Data2());
// 가장 최근에 생성 된 Memento를 가지고와서 상태 정보를 복원합니다.
info.RestorMemento(caretaker.pop());
// 상태 정보를 복원 한 후에 Information의 상태 정보를 출력합니다.
System.out.println("복구된 Data1 : " + info.get_Data1());
System.out.println("복구된 Data2 : " + info.get_Data2());
}
public static void main(String[] args) {
User user = new User();
user.exe();
}
}
▶ 실제로 Information의 정보를 다루는 User입니다. 10번 라인에서 특정 시점의 Information의 상태 정보를 저장하고 19번 라인에서 상태 정보를 복원하는 모습입니다.

참고 원본 : https://lktprogrammer.tistory.com/65
14 메멘토 패턴 (Memento Pattern)
메멘토 패턴 (Memento Pattern) 메멘토 패턴은 객체의 상태 정보를 저장하고 사용자의 필요에 의하여 원하는 시점의 데이터를 복원 할 수 있는 패턴을 의미합니다. ■메멘토 패턴 예제 구조 ▶ 실제
lktprogrammer.tistory.com