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

3 完整解决方案

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

银行账户结构图

       在图4中,Account充当环境类角色,AccountState充当抽象状态角色,NormalStateOverdraftStateRestrictedState充当具体状态角色。完整代码如下所示:

温馨提示:代码有点长,需要有耐心!大笑

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

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

[java]  view plain copy
  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. }  

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

段誉开户,初始金额为0.0

---------------------------------------------

段誉存款1000.0

现在余额为1000.0

现在帐户状态为NormalState

---------------------------------------------

段誉取款2000.0

现在余额为-1000.0

现在帐户状态为OverdraftState

---------------------------------------------

段誉存款3000.0

现在余额为2000.0

现在帐户状态为NormalState

---------------------------------------------

段誉取款4000.0

现在余额为-2000.0

现在帐户状态为RestrictedState

---------------------------------------------

段誉取款1000.0

帐号受限,取款失败

现在余额为-2000.0

现在帐户状态为RestrictedState

---------------------------------------------

计算利息!


【作者:刘伟 http://blog.csdn.net/lovelion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值