模式简析
具体实例
public class Demo1 {
public static void main(String[] args) {
GameRole g1 = new GameRole();
g1.GetInitState();
GameRole g2 = new GameRole();
g2.setVitality(g1.getVitality());
g2.setAttack(g1.getAttack());
g2.setDefense(g1.getDefense());
g1.DisplayState();
g1.Fight();
g1.DisplayState();
System.out.println("回复保存时的状态");
g1.setVitality(g2.getVitality());
g1.setAttack(g2.getAttack());
g1.setDefense(g2.getDefense());
g1.DisplayState();
}
}
class GameRole{
private int vitality;
private int attack;
private int defense;
public int getVitality() {
return vitality;
}
public void setVitality(int vitality) {
this.vitality = vitality;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public void DisplayState() {
System.out.println("游戏角色当前状态:\n");
System.out.println("生命力:"+vitality+"\n");
System.out.println("攻击力:"+attack+"\n");
System.out.println("防御力:"+defense+"\n");
}
public void GetInitState() {
this.vitality=100;
this.attack=100;
this.defense=100;
}
public void Fight() {
this.vitality=0;
this.attack=0;
this.defense=0;
}
}
public class Demo2 {
public static void main(String[] args) {
Originator o = new Originator();
o.setState("状态1");
o.showState();
Caretaker c =new Caretaker();
c.setMemento(o.createMemento());
o.setState("状态2");
o.showState();
o.setMemenot(c.getMemento());
o.showState();
}
}
class Memento{
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
class Originator{
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Memento createMemento(){
return new Memento(this.state);
}
public void setMemenot(Memento memento) {
state = memento.getState();
}
public void showState() {
System.out.println("当前状态:"+state);
}
}
class Caretaker{
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
public class Demo1 {
public static void main(String[] args) {
GameRole g1 = new GameRole();
g1.GetInitState();
g1.DisplayState();
RoleCaretaker rc =new RoleCaretaker();
rc.setRm(g1.SaveState());
g1.Fight();
g1.DisplayState();
g1.RecoverState(rc.getRm());
g1.DisplayState();
}
}
class GameRole{
private int vitality;
private int attack;
private int defense;
public int getVitality() {
return vitality;
}
public void setVitality(int vitality) {
this.vitality = vitality;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public void DisplayState() {
System.out.println("游戏角色当前状态:\n");
System.out.println("生命力:"+vitality+"\n");
System.out.println("攻击力:"+attack+"\n");
System.out.println("防御力:"+defense+"\n");
}
public void GetInitState() {
this.vitality=100;
this.attack=100;
this.defense=100;
}
public void Fight() {
this.vitality=0;
this.attack=0;
this.defense=0;
}
public RoleMemento SaveState() {
return new RoleMemento(vitality, attack, defense);
}
public void RecoverState(RoleMemento rm) {
this.vitality=rm.getVitality();
this.attack=rm.getAttack();
this.defense=rm.getDefense();
}
}
class RoleMemento{
private int vitality;
private int attack;
private int defense;
public RoleMemento(int vitality, int attack, int defense) {
super();
this.vitality = vitality;
this.attack = attack;
this.defense = defense;
}
public int getVitality() {
return vitality;
}
public void setVitality(int vitality) {
this.vitality = vitality;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
}
class RoleCaretaker{
private RoleMemento rm;
public RoleMemento getRm() {
return rm;
}
public void setRm(RoleMemento rm) {
this.rm = rm;
}
}