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

来源:互联网 发布:汕头 知乎 编辑:程序博客网 时间:2024/06/06 09:10

完整解决方案

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

银行账户结构图

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

//银行账户:环境类  class Account {      private AccountState state; //维持一个对抽象状态对象的引用      private String owner; //开户名      private double balance = 0; //账户余额            public Account(String owner,double init) {          this.owner = owner;          this.balance = balance;          this.state = new NormalState(this); //设置初始状态          System.out.println(this.owner + "开户,初始金额为" + init);           System.out.println("---------------------------------------------");          }            public double getBalance() {          return this.balance;      }            public void setBalance(double balance) {          this.balance = balance;      }            public void setState(AccountState state) {          this.state = state;      }            public void deposit(double amount) {          System.out.println(this.owner + "存款" + amount);          state.deposit(amount); //调用状态对象的deposit()方法          System.out.println("现在余额为"+ this.balance);          System.out.println("现在帐户状态为"+ this.state.getClass().getName());          System.out.println("---------------------------------------------");                  }            public void withdraw(double amount) {          System.out.println(this.owner + "取款" + amount);          state.withdraw(amount); //调用状态对象的withdraw()方法          System.out.println("现在余额为"+ this.balance);          System.out.println("现在帐户状态为"+ this. state.getClass().getName());                  System.out.println("---------------------------------------------");      }            public void computeInterest()      {          state.computeInterest(); //调用状态对象的computeInterest()方法      }  }    //抽象状态类  abstract class AccountState {      protected Account acc;      public abstract void deposit(double amount);      public abstract void withdraw(double amount);      public abstract void computeInterest();      public abstract void stateCheck();  }    //正常状态:具体状态类  class NormalState extends AccountState {      public NormalState(Account acc) {          this.acc = acc;      }    public NormalState(AccountState state) {          this.acc = state.acc;      }                public void deposit(double amount) {          acc.setBalance(acc.getBalance() + amount);          stateCheck();      }            public void withdraw(double amount) {          acc.setBalance(acc.getBalance() - amount);          stateCheck();      }            public void computeInterest()      {          System.out.println("正常状态,无须支付利息!");      }            //状态转换      public void stateCheck() {          if (acc.getBalance() > -2000 && acc.getBalance() <= 0) {              acc.setState(new OverdraftState(this));          }          else if (acc.getBalance() == -2000) {              acc.setState(new RestrictedState(this));          }          else if (acc.getBalance() < -2000) {              System.out.println("操作受限!");          }         }     }      //透支状态:具体状态类  class OverdraftState extends AccountState  {      public OverdraftState(AccountState state) {          this.acc = state.acc;      }            public void deposit(double amount) {          acc.setBalance(acc.getBalance() + amount);          stateCheck();      }            public void withdraw(double amount) {          acc.setBalance(acc.getBalance() - amount);          stateCheck();      }            public void computeInterest() {          System.out.println("计算利息!");      }            //状态转换      public void stateCheck() {          if (acc.getBalance() > 0) {              acc.setState(new NormalState(this));          }          else if (acc.getBalance() == -2000) {              acc.setState(new RestrictedState(this));          }          else if (acc.getBalance() < -2000) {              System.out.println("操作受限!");          }      }  }    //受限状态:具体状态类  class RestrictedState extends AccountState {      public RestrictedState(AccountState state) {          this.acc = state.acc;      }            public void deposit(double amount) {          acc.setBalance(acc.getBalance() + amount);          stateCheck();      }            public void withdraw(double amount) {          System.out.println("帐号受限,取款失败");      }            public void computeInterest() {          System.out.println("计算利息!");      }            //状态转换      public void stateCheck() {          if(acc.getBalance() > 0) {              acc.setState(new NormalState(this));          }          else if(acc.getBalance() > -2000) {              acc.setState(new OverdraftState(this));          }      }  }  
     编写如下客户端测试代码:
class Client {      public static void main(String args[]) {          Account acc = new Account("段誉",0.0);          acc.deposit(1000);          acc.withdraw(2000);          acc.deposit(3000);          acc.withdraw(4000);          acc.withdraw(1000);          acc.computeInterest();      }  }  

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

段誉开户,初始金额为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
原创粉丝点击