1. 程式人生 > 其它 >軟體設計-狀態模式

軟體設計-狀態模式

銀行賬戶
用Java程式碼模擬實現課堂上的“銀行賬戶”的例項,要求編寫客戶端測試程式碼模擬使用者存款和取款,注意賬戶物件狀態和行為的變化。

類圖

原始碼

Java

package rjsj.no22;

public class GreenState extends AccountState{

    public GreenState(double balance,Account acc) {
        this.balance = balance;
        this.account = acc;
        this.stateName = "正常狀態";
    }
}
package rjsj.no22;

public class RedState extends AccountState{

    public RedState(AccountState state) {
        this.balance = state.balance;
        this.account = state.account;
        this.stateName="透支狀態";
    }

    public void withdraw(double amount) {
        System.out.println(account.getOwner()
+"取款"+amount); System.out.println("抱歉,您的賬戶已處於透支狀態,不能取款!"); } }
package rjsj.no22;

public class YellowState extends AccountState{

    public YellowState(AccountState state) {
        this.balance = state.balance;
        this.account = state.account;
        this.stateName="欠費狀態";
    }
}
package rjsj.no22;

public class Account {
    private AccountState state;
    private String owner;

    public AccountState getState() {
        return state;
    }

    public void setState(AccountState state) {
        this.state = state;
    }

    public String getOwner() {
        return owner;
    }

    public void deposit(double amount) {
        state.deposit(amount);
    }

    public void withdraw(double amount) {
        state.withdraw(amount);
    }

    public Account(String owner, double initBalance) {
        this.owner = owner;
        this.state = new GreenState(initBalance,this);
        System.out.println(this.owner + "開戶成功!銀行卡初始金額:" + initBalance);
        System.out.println("----------------------------------------------------");
    }
}
package rjsj.no22;

public abstract class AccountState {
    protected Account account;//賬戶名
    protected double balance;//賬戶金額
    protected String stateName;//當前狀態

    public void deposit(double amount) {
        System.out.println(account.getOwner() + "存款" + amount);
        this.balance = this.balance + amount;
        stateCheck();
        System.out.println("賬戶餘額:" + this.balance);
        System.out.println("賬戶狀態:" + account.getState().stateName);
    }

    public void withdraw(double amount) {
        System.out.println(account.getOwner() + "取款" + amount);
        this.balance = this.balance - amount;
        stateCheck();
        System.out.println("賬戶餘額:" + this.balance);
        System.out.println("賬戶狀態:" + account.getState().stateName);
    }

    public void stateCheck(){//狀態審查
        if( balance >= -1000 && balance < 0 ) {
            account.setState(new YellowState(this));
        }else if( balance < -1000 ) {
            account.setState(new RedState(this));
        }else if ( balance > 0 ){
            account.setState(new GreenState(this.balance,this.account));
        }
    }
}
package rjsj.no22;

public class Client {

    public static void main(String[] args) {
        Account account = new Account("張三",100);
        account.deposit(8888);
        System.out.println("------------------------------");
        account.withdraw(666);
        System.out.println("------------------------------");
        account.deposit(50);
        System.out.println("------------------------------");
        account.withdraw(9000);
        System.out.println("------------------------------");
        account.withdraw(50000);
        System.out.println("------------------------------");
        account.deposit(3000);
        System.out.println("------------------------------");
        account.withdraw(100);
    }
}

C++

#include<iostream>
using namespace std;
class Account;
class AccountState{
public:
    Account *acc;
   double balance;
   string stateName;
public:
     virtual void stateCheck()=0;
     void deposit(double amount);
     virtual void withdraw(double amount);
};
class Account{
private:
    AccountState *state;
    string owner;
public:
    Account(string owner,double init);

    void setState(AccountState *state) {
        this->state=state;
    }
    AccountState* getState() {
        return this->state;
    }
    string getOwner() {
        return this->owner;
    }
    void deposit(double amount) {
        state->deposit(amount);
    }
    void withdraw(double amount) {
        state->withdraw(amount);
    }
};
class RedState :public AccountState{
public:
    RedState(AccountState *state) {
        this->balance = state->balance;
        this->acc = state->acc;
        this->stateName="透支狀態";
    }
    void withdraw(double amount){cout<<"賬戶處於透支狀態,不能取款!"<<endl;}
    void stateCheck();
};
class YellowState :public AccountState{
public:
    YellowState(AccountState *state) {
         this->balance = state->balance;
         this->acc = state->acc;
         this->stateName="欠費狀態";
    }
    void stateCheck();
};
class GreenState:public AccountState{
public:
     GreenState(double balance,Account *acc) {
        this->balance = balance;
        this->acc = acc;
        this->stateName="正常狀態";
    }
    GreenState(AccountState *state) {
        this->acc=state->acc;
        this->balance=state->balance;
        this->stateName="正常狀態";
    }
    void stateCheck() {
        if(balance>=-1000&&balance<0) {
            acc->setState(new YellowState(this));
        }else if(balance<-1000) {
            acc->setState(new RedState(this));
        }
        else{
            acc->setState(new GreenState(this));
        }
    }
};
void RedState::stateCheck(){
    if(balance>=-1000&&balance<0) {
            acc->setState(new YellowState(this));
        }else if(balance<-1000) {
            acc->setState(new RedState(this));
        }
        else {
            acc->setState(new GreenState(this));
        }
}
void YellowState::stateCheck() {
        if(balance>=-1000&&balance<0) {
            acc->setState(new YellowState(this));
        }else if(balance<-1000) {
            acc->setState(new RedState(this));
        }
        else{
            acc->setState(new GreenState(this));
        }
}
void AccountState::deposit(double amount){
        cout<<acc->getOwner()<<"存款"<<amount<<endl;
        this->balance+=amount;
        stateCheck();
        cout<<"賬戶餘額:"<<this->balance<<endl;
        cout<<"賬戶狀態:"<<acc->getState()->stateName<<endl;
    }
void AccountState::withdraw(double amount){
        cout<<acc->getOwner()<<"取款"<<amount<<endl;
        this->balance-=amount;
        stateCheck();
        cout<<"賬戶餘額:"<<this->balance<<endl;
        cout<<"賬戶狀態:"<<acc->getState()->stateName<<endl;
    }
Account::Account(string owner,double init){
    this->owner=owner;
    this->state=new GreenState(init,this);
    cout<<"恭喜"<<this->owner<<"開戶成功!銀行卡初始金額:"<<init<<endl;
    cout<<"--------------------------------------------------"<<endl;
}
int main(){
        Account *account=new Account("張三",100);
        account->deposit(100);
        cout<<"--------------------------------------------------"<<endl;
        account->withdraw(1000);
        cout<<"--------------------------------------------------"<<endl;
        account->deposit(1000);
        cout<<"--------------------------------------------------"<<endl;
        account->withdraw(2000);
        cout<<"--------------------------------------------------"<<endl;
        account->withdraw(10000);
        cout<<"--------------------------------------------------"<<endl;
        account->withdraw(1);
        cout<<"--------------------------------------------------"<<endl;
        account->deposit(100000);
        cout<<"--------------------------------------------------"<<endl;
        return 0;
}

執行結果: