Memento Pattern
This commit is contained in:
32
design-pattern/src/Memento/Application.java
Normal file
32
design-pattern/src/Memento/Application.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package Memento;
|
||||||
|
|
||||||
|
import Memento.abc.Memento;
|
||||||
|
import Memento.abc.Originator;
|
||||||
|
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
public class Application {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Stack<Memento> mementos = new Stack<>();
|
||||||
|
|
||||||
|
Originator originator = new Originator();
|
||||||
|
originator.setState("state 1");
|
||||||
|
mementos.push(originator.createMemento());
|
||||||
|
originator.setState("state 2");
|
||||||
|
mementos.push(originator.createMemento());
|
||||||
|
originator.setState("state 3");
|
||||||
|
mementos.push(originator.createMemento());
|
||||||
|
originator.setState("state Final");
|
||||||
|
mementos.push(originator.createMemento());
|
||||||
|
|
||||||
|
originator.restoreMemento(mementos.pop());
|
||||||
|
System.out.println(originator.getState());
|
||||||
|
originator.restoreMemento(mementos.pop());
|
||||||
|
System.out.println(originator.getState());
|
||||||
|
originator.restoreMemento(mementos.pop());
|
||||||
|
System.out.println(originator.getState());
|
||||||
|
originator.restoreMemento(mementos.pop());
|
||||||
|
System.out.println(originator.getState());
|
||||||
|
}
|
||||||
|
}
|
||||||
5
design-pattern/src/Memento/Memento
Normal file
5
design-pattern/src/Memento/Memento
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
Memento Pattern
|
||||||
|
- 상태 저장하기
|
||||||
|
- 메멘토 패턴을 통해 객체의 상태를 저장하고 이전상태로 복구
|
||||||
|
|
||||||
|
|
||||||
14
design-pattern/src/Memento/abc/Memento.java
Normal file
14
design-pattern/src/Memento/abc/Memento.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package Memento.abc;
|
||||||
|
|
||||||
|
public class Memento {
|
||||||
|
|
||||||
|
String state;
|
||||||
|
|
||||||
|
protected Memento(String state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getState() {
|
||||||
|
return this.state;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
design-pattern/src/Memento/abc/Originator.java
Normal file
22
design-pattern/src/Memento/abc/Originator.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package Memento.abc;
|
||||||
|
|
||||||
|
public class Originator {
|
||||||
|
|
||||||
|
String state;
|
||||||
|
|
||||||
|
public Memento createMemento() {
|
||||||
|
return new Memento(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void restoreMemento(Memento memento) {
|
||||||
|
this.state = memento.getState();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setState(String state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user