forked from luisburgos/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package memento.examples.timemachine; | ||
|
||
public class Life { | ||
private String time; | ||
|
||
public void set(String time) { | ||
System.out.println("Setting time to " + time); | ||
this.time = time; | ||
} | ||
|
||
public Memento saveToMemento() { | ||
System.out.println("Saving time to Memento"); | ||
return new Memento(time); | ||
} | ||
|
||
public void restoreFromMemento(Memento memento) { | ||
time = memento.getSavedTime(); | ||
System.out.println("Time restored from Memento: " + time); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package memento.examples.timemachine; | ||
|
||
public class Memento { | ||
|
||
private final String time; | ||
|
||
public Memento(String timeToSave) { | ||
time = timeToSave; | ||
} | ||
|
||
public String getSavedTime() { | ||
return time; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package memento.examples.timemachine; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TimeMachineClient { | ||
|
||
public static void main(String[] args) { | ||
|
||
List<Memento> savedTimes = new ArrayList<Memento>(); | ||
|
||
Life life = new Life(); | ||
|
||
//time travel and record the eras | ||
life.set("1000 B.C."); | ||
savedTimes.add(life.saveToMemento()); | ||
life.set("1000 A.D."); | ||
savedTimes.add(life.saveToMemento()); | ||
life.set("2000 A.D."); | ||
savedTimes.add(life.saveToMemento()); | ||
life.set("4000 A.D."); | ||
|
||
life.restoreFromMemento(savedTimes.get(0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package memento.pattern; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Caretaker { | ||
|
||
private List<Memento> savedStates = new ArrayList<Memento>(); | ||
|
||
public void addMemento(Memento m) { | ||
savedStates.add(m); | ||
} | ||
|
||
public Memento getMemento(int index) { | ||
return savedStates.get(index); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package memento.pattern; | ||
|
||
public class Memento { | ||
|
||
private String state; | ||
|
||
public Memento(String stateToSave){ | ||
state = stateToSave; | ||
} | ||
|
||
public String getSavedState(){ | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package memento.pattern; | ||
|
||
public class MementoTest { | ||
|
||
public static void main(String[] args) { | ||
Caretaker caretaker = new Caretaker(); | ||
|
||
Originator originator = new Originator(); | ||
originator.set("State1"); | ||
originator.set("State2"); | ||
caretaker.addMemento( originator.saveToMemento() ); | ||
originator.set("State3"); | ||
caretaker.addMemento( originator.saveToMemento() ); | ||
originator.set("State4"); | ||
|
||
originator.restoreFromMemento( caretaker.getMemento(1) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package memento.pattern; | ||
|
||
public class Originator { | ||
|
||
private String state; | ||
|
||
public void set(String state) { | ||
System.out.println("Originator: Setting state to "+state); | ||
this.state = state; | ||
} | ||
|
||
public Memento saveToMemento(){ | ||
System.out.println("Originator: Saving to Memento."); | ||
return new Memento(state); | ||
} | ||
|
||
public void restoreFromMemento(Memento m) { | ||
state = m.getSavedState(); | ||
System.out.println("Originator: State after restoring from Memento: "+state); | ||
} | ||
} |