> For the complete documentation index, see [llms.txt](https://blakes-organization.gitbook.io/rainsister/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blakes-organization.gitbook.io/rainsister/design-pattern/memento-pattern.md).

# Memento Pattern

일반적으로 개체의 특정 상태를 저장하여 적절한 시간에 개체를 복원할 수 있도록 할때 사용할수 있음. 키보드에서 ctrl + z 기능과 같은거?

아래 예시를 보면 A 에 다양한 속성이 있으며 A는 필요를 결정할수 있음. Backup 속성인 메모 클래스 B는 A의 일부 내부상태를 저장하고 클래스 C는 메모를 저장만 하고 수정불가.

## 코드예시

```java
public class Original {
	
	private String value;
	
	public String getValue() {
		return value;
	}
 
	public void setValue(String value) {
		this.value = value;
	}
 
	public Original(String value) {
		this.value = value;
	}
 
	public Memento createMemento(){
		return new Memento(value);
	}
	
	public void restoreMemento(Memento memento){
		this.value = memento.getValue();
	}
}
```

```java
public class Memento {
	
	private String value;
 
	public Memento(String value) {
		this.value = value;
	}
 
	public String getValue() {
		return value;
	}
 
	public void setValue(String value) {
		this.value = value;
	}
}
```

```java
public class Storage {
	
	private Memento memento;
	
	public Storage(Memento memento) {
		this.memento = memento;
	}
 
	public Memento getMemento() {
		return memento;
	}
 
	public void setMemento(Memento memento) {
		this.memento = memento;
	}
}
```

```java
public class Test {
 
	public static void main(String[] args) {
		
		Original origi = new Original("egg");
 
		Storage storage = new Storage(origi.createMemento());
 
		System.out.println("초기화 상태：" + origi.getValue());
		origi.setValue("niu");
		System.out.println("수정후 상태：" + origi.getValue());
 
		origi.restoreMemento(storage.getMemento());
		System.out.println("되돌린후 최종 상태：" + origi.getValue());
	}
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blakes-organization.gitbook.io/rainsister/design-pattern/memento-pattern.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
