Last active
April 14, 2019 21:11
-
-
Save jonepl/861432e0fe27e8a0b46a215de9b3dd95 to your computer and use it in GitHub Desktop.
Memento Design Pattern
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
package learning.dp.memento; | |
public class Driver { | |
public static void main (String [] args) { | |
Orginator o1 = new Orginator(); | |
o1.setName("Purnell Jones"); | |
o1.setHp(16.197); | |
o1.setSpecialPhrase("Naw Bruh!"); | |
o1.speak(); | |
Memento m1 = o1.makeMemento(); | |
o1.setName("Spencer hasNoLaptop"); | |
o1.setHp(1.9); | |
o1.setSpecialPhrase("Fuck you"); | |
o1.speak(); | |
o1.restoreFromMemento(m1); | |
o1.speak(); | |
} | |
} |
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
package learning.dp.memento; | |
interface Memento { } |
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
package learning.dp.memento; | |
public class Orginator { | |
private String name; | |
private double hp; | |
private String specialPhrase; | |
private class PlayerMemento implements Memento { | |
private String nameCopy; | |
private double hpCopy; | |
private String specialPhraseCopy; | |
//Constructor | |
public PlayerMemento() { | |
nameCopy = name; | |
hpCopy = hp; | |
specialPhraseCopy = specialPhrase; | |
} | |
void restore(Orginator o) { | |
if( o != Orginator.this ) { | |
throw new RuntimeException("Damn Bruh."); | |
} | |
o.name = nameCopy; | |
o.hp = hpCopy; | |
o.specialPhrase = specialPhraseCopy; | |
} | |
}; // End PlayerMemento | |
public Memento makeMemento() { | |
//System.out.println("Inside Orginator::storeInMemento()"); | |
return new PlayerMemento(); | |
} | |
public void restoreFromMemento(Memento m) { | |
((PlayerMemento)m).restore(this); | |
} | |
public void speak() { | |
System.out.println("My name is " + name + | |
". I have " + hp + " hp. " + specialPhrase ); | |
} | |
//=======================================// | |
// Accessor and Mutators | |
//=======================================// | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public double getHp() { | |
return hp; | |
} | |
public void setHp(double hp) { | |
this.hp = hp; | |
} | |
public String getSpecialPhrase() { | |
return specialPhrase; | |
} | |
public void setSpecialPhrase(String specialPhrase) { | |
this.specialPhrase = specialPhrase; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment