1. // 角色
    2. public class Role {
    3. // 体力、攻击、防御
    4. private int vit;
    5. private int ack;
    6. private int def;
    7. // 状态显示
    8. public void StateDisplay(){
    9. System.out.println("角色当前状态:");
    10. System.out.println("体力:"+this.vit);
    11. System.out.println("攻击:"+this.ack);
    12. System.out.println("防御:"+this.def);
    13. }
    14. // 获得初始状态,通常来自本机磁盘或远程数据库
    15. public void GetInitState(){
    16. this.vit=100;
    17. this.ack=100;
    18. this.def=100;
    19. }
    20. // 战斗
    21. public void Fight(){
    22. this.vit=0;
    23. this.ack=0;
    24. this.def=0;
    25. }
    26. // 保存角色状态
    27. public RoleStateMemento SaveState(){
    28. return new RoleStateMemento(this.vit, this.ack, this.def);
    29. }
    30. // 恢复角色状态
    31. public void RevoceryState(RoleStateMemento memento){
    32. this.vit = memento.getVit();
    33. this.ack = memento.getAtk();
    34. this.def = memento.getDef();
    35. }
    36. public int getVit() {
    37. return vit;
    38. }
    39. public void setVit(int vit) {
    40. this.vit = vit;
    41. }
    42. public int getAck() {
    43. return ack;
    44. }
    45. public void setAck(int ack) {
    46. this.ack = ack;
    47. }
    48. public int getDef() {
    49. return def;
    50. }
    51. public void setDef(int def) {
    52. this.def = def;
    53. }
    54. }
    55. // 角色状态管理者类
    56. public class RoleStateCaretaker {
    57. public RoleStateMemento roleStateMemento;
    58. }
    59. // 角色状态存储类
    60. public class RoleStateMemento {
    61. private int vit;
    62. private int atk;
    63. private int def;
    64. public RoleStateMemento(int vit, int atk, int def) {
    65. super();
    66. this.vit = vit;
    67. this.atk = atk;
    68. this.def = def;
    69. }
    70. public int getVit() {
    71. return vit;
    72. }
    73. public void setVit(int vit) {
    74. this.vit = vit;
    75. }
    76. public int getAtk() {
    77. return atk;
    78. }
    79. public void setAtk(int atk) {
    80. this.atk = atk;
    81. }
    82. public int getDef() {
    83. return def;
    84. }
    85. public void setDef(int def) {
    86. this.def = def;
    87. }
    88. }
    89. // 角色状态管理者类
    90. public class RoleStateCaretaker {
    91. // 角色状态
    92. public RoleStateMemento roleStateMemento;
    93. }
    94. // 角色状态存储类
    95. public class RoleStateMemento {
    96. private int vit;
    97. private int atk;
    98. private int def;
    99. public RoleStateMemento(int vit, int atk, int def) {
    100. super();
    101. this.vit = vit;
    102. this.atk = atk;
    103. this.def = def;
    104. }
    105. public int getVit() {
    106. return vit;
    107. }
    108. public void setVit(int vit) {
    109. this.vit = vit;
    110. }
    111. public int getAtk() {
    112. return atk;
    113. }
    114. public void setAtk(int atk) {
    115. this.atk = atk;
    116. }
    117. public int getDef() {
    118. return def;
    119. }
    120. public void setDef(int def) {
    121. this.def = def;
    122. }
    123. }
    124. public class Test {
    125. public static void main(String[] args) {
    126. // 大战Boss前
    127. Role role = new Role();
    128. role.GetInitState();
    129. role.StateDisplay();
    130. // 存档
    131. RoleStateCaretaker stateAdmin = new RoleStateCaretaker();
    132. stateAdmin.roleStateMemento=role.SaveState();
    133. // 大战boss,没了
    134. role.Fight();
    135. role.StateDisplay();
    136. // 回档
    137. role.RevoceryState(stateAdmin.roleStateMemento);
    138. role.StateDisplay();
    139. }
    140. }