• 设计模式之状态模式


    题目

    [实验任务一]:银行账户
    用Java代码模拟实现课堂上的“银行账户”的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。

    类图

    C++

    Account.h

    #pragma once
    #ifndef _ACCOUNT_H_
    #define _ACCOUNT_H_
    #include<iostream>
    #include<string>
    #include<list>
    using namespace std;
    
    class AccountState;
    class Account {
    private:
    	string owner;
    	AccountState* state;
    public:
    	Account(string owner, double init);
    	~Account();
    	void Deposit(double amount);
    	void Withdraw(double amount);
    	void SetState(AccountState* state);
    };
    
    
    #endif
    
    

    AccountState.h

    #pragma once
    #ifndef _ACCOUNTSTATE_H_
    #define _ACCOUNTSTATE_H_
    #include<string>
    using namespace std;
    class Account;
    class AccountState {
    public:
    	virtual ~AccountState();
    	AccountState();
    	virtual void Deposit(double amount) = 0;
    	virtual void Withdraw(double amount) = 0;
    	virtual void StateCheck() = 0;
    	double balance;
    	Account* acc;
    };
    class GreenState :public AccountState {
    public:
    	GreenState(AccountState* acc);
    	GreenState(double balance, Account* acc);
    	~GreenState();
    	void Deposit(double amount);
    	void Withdraw(double amount);
    	void StateCheck();
    
    };
    
    class YellowState :public AccountState {
    public:
    	YellowState(AccountState* acc);
    	~YellowState();
    	void Deposit(double amount);
    	void Withdraw(double amount);
    	void StateCheck();
    
    };
    
    class RedState :public AccountState {
    public:
    	RedState(AccountState* acc);
    	~RedState();
    	void Deposit(double amount);
    	void Withdraw(double amount);
    	void StateCheck();
    
    };
    #endif
    

    Account.cpp

    #include"Account.h"
    #include"AccountState.h"
    #include<iostream>
    #include<string>
    #include<list>
    #include<cstring>
    #include<sstream>
    using namespace std;
    
    Account::Account(string owner, double init) {
    	this->owner = owner;
    	this->state = new GreenState(init, this);
    	cout << "账户:" << this->owner << "初始金额" << init << endl;
    }
    
    Account::~Account() {}
    
    void Account::SetState(AccountState* state) {
    	this->state = state;
    }
    
    void Account::Deposit(double amount) {
    
    	state->Deposit(amount);
    	cout << "现金余额为" << this->state->balance << endl;
    	
    }
    
    void Account::Withdraw(double amount) {
    	state->Withdraw(amount);
    	cout << "现金余额为" << this->state->balance << endl;
    }
    

    AccountState.cpp

    #include"Account.h"
    #include"AccountState.h"
    #include<iostream>
    using namespace std;
    
    AccountState::AccountState() {}
    
    AccountState::~AccountState() {}
    
    void AccountState::Withdraw(double amount) {}
    
    void AccountState::Deposit(double amount) {}
    
    void AccountState::StateCheck() {}
    
    GreenState::GreenState(AccountState* state) {
    	this->balance = state->balance;
    	this->acc = state->acc;
    }
    
    GreenState::GreenState(double balance, Account* acc)
    {
    	this->balance = balance;
    	this->acc = acc;
    }
    GreenState::~GreenState()
    {
    }
    void GreenState::StateCheck() {
    	if (balance >= 0) {
    		acc->SetState(new GreenState(this));
    		cout << "当前为正常(绿色)状态" << endl;
    	}
    	else if (balance < 0 && balance >= -1000) {
    		acc->SetState(new YellowState(this));
    		cout << "当前为欠费(黄色)状态" << endl;
    	}
    	else {
    		acc->SetState(new RedState(this));
    		cout << "当前透支(红色)状态" << endl;
    	}
    }
    void GreenState::Deposit(double amount)
    {
    	this->balance += amount;
    	StateCheck();
    }
    
    void GreenState::Withdraw(double amount)
    {
    	this->balance -= amount;
    	StateCheck();
    }
    
    YellowState::YellowState(AccountState* state) {
    	this->balance = state->balance;
    	this->acc = state->acc;
    }
    
    YellowState::~YellowState() {}
    
    void YellowState::StateCheck() {
    	if (balance >= 0) {
    		acc->SetState(new GreenState(this));
    		cout << "当前为正常(绿色)状态" << endl;
    	}
    	else if (balance < 0 && balance >= -1000) {
    		acc->SetState(new YellowState(this));
    		cout << "当前为欠费(黄色)状态" << endl;
    	}
    	else {
    		acc->SetState(new RedState(this));
    		cout << "当前透支(红色)状态" << endl;
    	}
    }
    
    void YellowState::Deposit(double amount) {
    	balance += amount;
    	StateCheck();
    }
    
    void YellowState::Withdraw(double amount) {
    	balance -= amount;
    	StateCheck();
    }
    
    RedState::RedState(AccountState* state)
    {
    	this->balance = state->balance;
    	this->acc = state->acc;
    }
    RedState::~RedState()
    {
    }
    void RedState::StateCheck() {
    	if (balance >= 0) {
    		acc->SetState(new GreenState(this));
    		cout << "当前为正常(绿色)状态" << endl;
    	}
    	else if (balance < 0 && balance >= -1000) {
    		acc->SetState(new YellowState(this));
    		cout << "当前为欠费(黄色)状态" << endl;
    	}
    	else {
    		acc->SetState(new RedState(this));
    		cout << "当前透支(红色)状态" << endl;
    	}
    }
    void RedState::Deposit(double amount)
    {
    	this->balance += amount;
    	StateCheck();
    }
    
    void RedState::Withdraw(double amount)
    {
    	cout << "透支状态,无法取款" << endl;
    }
    

    main.cpp

    #include<iostream>
    #include"Account.h"
    #include"AccountState.h"
    using namespace std;
    
    int main() {
    	Account* acc = new Account("gazikel", 2500);
    
    	cout << "==========绿色状态存钱===========" << endl;
    	acc->Deposit(1500);
    	cout << "------------------------------" << endl;
    
    	// 绿色状态取钱
    	cout << "==========绿色状态取钱===========" << endl;
    	acc->Withdraw(4500);
    	cout << "------------------------------" << endl;
    	// 黄色状态存钱
    	cout << "==========黄色状态存钱===========" << endl;
    	acc->Deposit(100);
    	cout << "------------------------------" << endl;
    	// 黄色状态取钱
    	cout << "==========黄色状态取钱===========" << endl;
    	acc->Withdraw(700);
    	cout << "------------------------------" << endl;
    	// 红色状态取钱
    	cout << "==========红色状态取钱===========" << endl;
    	acc->Withdraw(100);
    	// 红色状态存钱
    	cout << "==========红色状态存钱===========" << endl;
    	acc->Deposit(1000);
    }
    


    Java

    Account类:

    package com.gazikel;
    
    public class Account {
    
        private AccountState state;
        private String owner;
    
        public Account(String owner, double init) {
            this.owner = owner;
            state = new GreenState(init, this);
            System.out.println("账户:" + this.owner + ",余额:" + init);
    
        }
    
        public void setState(AccountState state) {
            this.state = state;
        }
    
        /**
         * 存款
         * @param amount
         */
        public void deposit(double amount) {
            state.deposit(amount);
            System.out.println("现金余额为" + this.state.balance);
        }
    
    
        /**
         * 取款
         * @param amount
         */
        public void withdraw(double amount){
            state.withdraw(amount);
            System.out.println("现金余额为" + state.balance);
        }
    }
    
    

    AccountState抽象类:

    package com.gazikel;
    
    /**
     * 状态抽象类
     */
    public abstract class AccountState {
    
        protected Account acc;
        protected double balance;
    
        public abstract void stateCheck();
    
        public abstract void deposit(double amount);
    
        public abstract void withdraw(double amount);
    }
    

    三个状态具体实现类

    GreenState

    package com.gazikel;
    
    public class GreenState extends AccountState {
    
        public GreenState() {}
    
        public GreenState(double amount, Account account) {
            balance = amount;
            acc = account;
        }
    
        @Override
        public void stateCheck() {
            if (balance >= 0) {
                acc.setState(new GreenState(balance, acc));
                System.out.println("当前为正常(绿色)状态");
            } else if(balance <0 && balance >= -1000) {
                acc.setState(new YellowState(balance, acc));
                System.out.println("当前为欠费(黄色)状态");
            } else {
                acc.setState(new RedState(balance, acc));
                System.out.println("当前透支(红色)状态");
            }
    
        }
    
        @Override
        public void deposit(double amount) {
            balance += amount;
            System.out.println(balance);
            stateCheck();
        }
    
        @Override
        public void withdraw(double amount) {
            balance -= amount;
            stateCheck();
        }
    }
    

    RedState:

    package com.gazikel;
    
    public class RedState extends AccountState {
    
        public RedState(double amount, Account acc) {
            this.balance = amount;
            this.acc = acc;
        }
        @Override
        public void stateCheck() {
            if (balance >= 0) {
                acc.setState(new GreenState(balance, acc));
                System.out.println("当前为正常(绿色)状态");
            } else if(balance <0 && balance >= -1000) {
                acc.setState(new YellowState(balance, acc));
                System.out.println("当前为欠费(黄色)状态");
            } else {
                acc.setState(new RedState(balance, acc));
                System.out.println("当前透支(红色)状态");
            }
    
        }
    
        @Override
        public void deposit(double amount) {
            balance += amount;
            stateCheck();
        }
    
        @Override
        public void withdraw(double amount) {
            System.out.println("透支状态,无法取款!");
        }
    }
    

    YellowState

    package com.gazikel;
    
    public class YellowState extends AccountState {
    
        public YellowState(double amount, Account acc) {
            this.balance = amount;
            this.acc = acc;
        }
    
        @Override
        public void stateCheck() {
            if (balance >= 0) {
                acc.setState(new GreenState(balance, acc));
                System.out.println("当前为正常(绿色)状态");
            } else if(balance <0 && balance >= -1000) {
                acc.setState(new YellowState(balance, acc));
                System.out.println("当前为欠费(黄色)状态");
            } else {
                acc.setState(new RedState(balance, acc));
                System.out.println("当前透支(红色)状态");
            }
        }
    
        @Override
        public void deposit(double amount) {
            balance += amount;
            stateCheck();
    
        }
    
        @Override
        public void withdraw(double amount) {
            balance -= amount;
            stateCheck();
    
        }
    }
    

  • 相关阅读:
    Java 如何删除 List 中的重复元素
    显示源代码及其子目录中的文件结构
    关于overflow:hidden的作用(溢出隐藏,清除浮动,解决外边塌陷等等)
    网页制作小技巧
    StringWriter/PrintWriter在Java输出异常信息中的作用
    [转载]利用@media screen实现网页布局的自适应,@media screen and
    OutputCache祥解
    string和stringBuilder区别
    sql执行顺序
    sql事务(Transaction)用法介绍及回滚实例
  • 原文地址:https://www.cnblogs.com/Gazikel/p/15613819.html
Copyright © 2020-2023  润新知