处理对象的多种状态及其相互转换——状态模式(三)

3 完整解决方案

Sunny软件公司开发人员使用状态模式来解决账户状态的转换问题,客户端只需要执行简单的存款和取款操作,系统根据余额将自动转换到相应的状态,其基本结构如图4所示:

处理对象的多种状态及其相互转换——状态模式(三) - 图1

图4 银行账户结构图

在图4中,Account充当环境类角色,AccountState充当抽象状态角色,NormalState、OverdraftState和RestrictedState充当具体状态角色。完整代码如下所示: 温馨提示:代码有点长,需要有耐心!

  1. //银行账户:环境类
  2. class Account {
  3. private AccountState state; //维持一个对抽象状态对象的引用
  4. private String owner; //开户名
  5. private double balance = 0; //账户余额
  6. public Account(String owner,double init) {
  7. this.owner = owner;
  8. this.balance = balance;
  9. this.state = new NormalState(this); //设置初始状态
  10. System.out.println(this.owner + "开户,初始金额为" + init);
  11. System.out.println("---------------------------------------------");
  12. }
  13. public double getBalance() {
  14. return this.balance;
  15. }
  16. public void setBalance(double balance) {
  17. this.balance = balance;
  18. }
  19. public void setState(AccountState state) {
  20. this.state = state;
  21. }
  22. public void deposit(double amount) {
  23. System.out.println(this.owner + "存款" + amount);
  24. state.deposit(amount); //调用状态对象的deposit()方法
  25. System.out.println("现在余额为"+ this.balance);
  26. System.out.println("现在帐户状态为"+ this.state.getClass().getName());
  27. System.out.println("---------------------------------------------");
  28. }
  29. public void withdraw(double amount) {
  30. System.out.println(this.owner + "取款" + amount);
  31. state.withdraw(amount); //调用状态对象的withdraw()方法
  32. System.out.println("现在余额为"+ this.balance);
  33. System.out.println("现在帐户状态为"+ this. state.getClass().getName());
  34. System.out.println("---------------------------------------------");
  35. }
  36. public void computeInterest()
  37. {
  38. state.computeInterest(); //调用状态对象的computeInterest()方法
  39. }
  40. }
  41. //抽象状态类
  42. abstract class AccountState {
  43. protected Account acc;
  44. public abstract void deposit(double amount);
  45. public abstract void withdraw(double amount);
  46. public abstract void computeInterest();
  47. public abstract void stateCheck();
  48. }
  49. //正常状态:具体状态类
  50. class NormalState extends AccountState {
  51. public NormalState(Account acc) {
  52. this.acc = acc;
  53. }
  54. public NormalState(AccountState state) {
  55. this.acc = state.acc;
  56. }
  57. public void deposit(double amount) {
  58. acc.setBalance(acc.getBalance() + amount);
  59. stateCheck();
  60. }
  61. public void withdraw(double amount) {
  62. acc.setBalance(acc.getBalance() - amount);
  63. stateCheck();
  64. }
  65. public void computeInterest()
  66. {
  67. System.out.println("正常状态,无须支付利息!");
  68. }
  69. //状态转换
  70. public void stateCheck() {
  71. if (acc.getBalance() > -2000 && acc.getBalance() <= 0) {
  72. acc.setState(new OverdraftState(this));
  73. }
  74. else if (acc.getBalance() == -2000) {
  75. acc.setState(new RestrictedState(this));
  76. }
  77. else if (acc.getBalance() < -2000) {
  78. System.out.println("操作受限!");
  79. }
  80. }
  81. }
  82. //透支状态:具体状态类
  83. class OverdraftState extends AccountState
  84. {
  85. public OverdraftState(AccountState state) {
  86. this.acc = state.acc;
  87. }
  88. public void deposit(double amount) {
  89. acc.setBalance(acc.getBalance() + amount);
  90. stateCheck();
  91. }
  92. public void withdraw(double amount) {
  93. acc.setBalance(acc.getBalance() - amount);
  94. stateCheck();
  95. }
  96. public void computeInterest() {
  97. System.out.println("计算利息!");
  98. }
  99. //状态转换
  100. public void stateCheck() {
  101. if (acc.getBalance() > 0) {
  102. acc.setState(new NormalState(this));
  103. }
  104. else if (acc.getBalance() == -2000) {
  105. acc.setState(new RestrictedState(this));
  106. }
  107. else if (acc.getBalance() < -2000) {
  108. System.out.println("操作受限!");
  109. }
  110. }
  111. }
  112. //受限状态:具体状态类
  113. class RestrictedState extends AccountState {
  114. public RestrictedState(AccountState state) {
  115. this.acc = state.acc;
  116. }
  117. public void deposit(double amount) {
  118. acc.setBalance(acc.getBalance() + amount);
  119. stateCheck();
  120. }
  121. public void withdraw(double amount) {
  122. System.out.println("帐号受限,取款失败");
  123. }
  124. public void computeInterest() {
  125. System.out.println("计算利息!");
  126. }
  127. //状态转换
  128. public void stateCheck() {
  129. if(acc.getBalance() > 0) {
  130. acc.setState(new NormalState(this));
  131. }
  132. else if(acc.getBalance() > -2000) {
  133. acc.setState(new OverdraftState(this));
  134. }
  135. }
  136. }

编写如下客户端测试代码:

  1. class Client {
  2. public static void main(String args[]) {
  3. Account acc = new Account("段誉",0.0);
  4. acc.deposit(1000);
  5. acc.withdraw(2000);
  6. acc.deposit(3000);
  7. acc.withdraw(4000);
  8. acc.withdraw(1000);
  9. acc.computeInterest();
  10. }
  11. }

编译并运行程序,输出结果如下:

  1. 段誉开户,初始金额为0.0
  2. ---------------------------------------------
  3. 段誉存款1000.0
  4. 现在余额为1000.0
  5. 现在帐户状态为NormalState
  6. ---------------------------------------------
  7. 段誉取款2000.0
  8. 现在余额为-1000.0
  9. 现在帐户状态为OverdraftState
  10. ---------------------------------------------
  11. 段誉存款3000.0
  12. 现在余额为2000.0
  13. 现在帐户状态为NormalState
  14. ---------------------------------------------
  15. 段誉取款4000.0
  16. 现在余额为-2000.0
  17. 现在帐户状态为RestrictedState
  18. ---------------------------------------------
  19. 段誉取款1000.0
  20. 帐号受限,取款失败
  21. 现在余额为-2000.0
  22. 现在帐户状态为RestrictedState
  23. ---------------------------------------------
  24. 计算利息!