<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 處理對象的多種狀態及其相互轉換——狀態模式(三) 3 完整解決方案 Sunny軟件公司開發人員使用狀態模式來解決賬戶狀態的轉換問題,客戶端只需要執行簡單的存款和取款操作,系統根據余額將自動轉換到相應的狀態,其基本結構如圖4所示: ![](http://img.my.csdn.net/uploads/201301/20/1358693610_6618.jpg) 圖4 銀行賬戶結構圖 在圖4中,Account充當環境類角色,AccountState充當抽象狀態角色,NormalState、OverdraftState和RestrictedState充當具體狀態角色。完整代碼如下所示: 溫馨提示:代碼有點長,需要有耐心! ``` //銀行賬戶:環境類 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 --------------------------------------------- 計算利息! ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看