State pattern(模拟一个借记卡的例子)

participants

    The classes and/or objects participating in this pattern are:

  • Context  (Account)
    • defines the interface of interest to clients
    • maintains an instance of a ConcreteState subclass that defines the current state.
  • State  (State)
    • defines an interface for encapsulating the behavior associated with a particular state of the Context.
  • Concrete State  (RedState, SilverState, GoldState)
    • each subclass implements a behavior associated with a state of Context

模拟一个借记卡的例子,假设每次存款都有利息,并且不允许透支

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace State
{
    class Program
    {
        static void Main(string[] args)
        {
            Account account = new Account("张三丰");
            account.Deposit(500.0);
            account.Deposit(5000.0);
            account.Withdraw(5499);
            Console.ReadKey();
        }
    }

    abstract class State
    {

        protected Account account;
        protected double balance;
        protected double interest;
        protected double lowerLimit;
        protected double upperLimit;

        public Account Account
        {

            get { return account; }

            set { account = value; }

        }


        //余款
        public double Balance
        {

            get { return balance; }

            set { balance = value; }

        }

        public abstract void Deposit(double amount);

        public abstract void Withdraw(double amount);

        public abstract void PayInterest();

    }

    class RedState : State
    {

        private double _serviceFee;

        public RedState(State state)
        {

            this.balance = state.Balance;

            this.account = state.Account;

            Initialize();

        }

        private void Initialize()
        {

            // 正式应用时取自数据源或配置文件

            interest = 0.0;
            lowerLimit = -100.0;
            upperLimit = 0.0;
            _serviceFee = 15.00;

        }


        //存款
        public override void Deposit(double amount)
        {

            balance += amount;

            StateChangeCheck();
            PayInterest();
        }

        public override void Withdraw(double amount)
        {

            amount = amount - _serviceFee;

            Console.WriteLine("已透支,无法取款!");

        }

        public override void PayInterest()
        {

            // 没有利息

        }

        private void StateChangeCheck()
        {

            if (balance > upperLimit)
            {

                account.State = new SilverState(this);

            }

        }

    }

    class SilverState : State
    {

        public SilverState(State state) :

            this(state.Balance, state.Account)
        {

        }

        public SilverState(double balance, Account account)
        {

            this.balance = balance;

            this.account = account;

            Initialize();

        }

        private void Initialize()
        {

            interest = 0.02;
            lowerLimit = 0.0;
            upperLimit = 1000.0;
        }

        public override void Deposit(double amount)
        {

            balance += amount;
            StateChangeCheck();
            PayInterest();
        }

        public override void Withdraw(double amount)
        {

            double tempBalance = balance - amount;
            if (tempBalance > 0)
            {
                balance -= amount;
            }
            else
            {
                Console.WriteLine("已透支,无法取款!");
            }
            StateChangeCheck();

        }

        public override void PayInterest()
        {

            Console.WriteLine("利息= {0:C}", interest * balance);
            balance += interest * balance;
            StateChangeCheck();
        }

        private void StateChangeCheck()
        {

            if (balance < lowerLimit)
            {

                account.State = new RedState(this);

            }

            else if (balance > upperLimit)
            {

                account.State = new GoldState(this);

            }

        }

    }

    class GoldState : State
    {

        // Overloaded constructors

        public GoldState(State state)

            : this(state.Balance, state.Account)
        {

        }

        public GoldState(double balance, Account account)
        {

            this.balance = balance;

            this.account = account;

            Initialize();

        }

        private void Initialize()
        {

            interest = 0.05;
            lowerLimit = 1000.0;
            upperLimit = 10000.0;
        }

        public override void Deposit(double amount)
        {

            balance += amount;

            StateChangeCheck();
            PayInterest();
        }

        public override void Withdraw(double amount)
        {
            double tempBalance = balance - amount;
            if (tempBalance > 0)
            {
                balance -= amount;
            }
            else
            {
                Console.WriteLine("已透支,无法取款!");
            }

            StateChangeCheck();

        }

        public override void PayInterest()
        {


            Console.WriteLine("利息= {0:C}", interest * balance);
            balance += interest * balance;
            StateChangeCheck();

        }

        private void StateChangeCheck()
        {

            if (balance < 0.0)
            {

                account.State = new RedState(this);

            }

            else if (balance < lowerLimit)
            {

                account.State = new SilverState(this);

            }

        }

    }

    class Account
    {

        private State _state;

        private string _owner;

        // Constructor

        public Account(string owner)
        {

            // New accounts are 'Silver' by default

            this._owner = owner;

            this._state = new SilverState(0.0, this);

        }

        // Properties

        public double Balance
        {

            get { return _state.Balance; }

        }

        public State State
        {

            get { return _state; }

            set { _state = value; }

        }

        public void Deposit(double amount)
        {

            _state.Deposit(amount);

            Console.WriteLine("存款 {0:C} --- ", amount);

            Console.WriteLine(" 余额 = {0:C}", this.Balance);

            Console.WriteLine(" 状态 = {0}",

              this.State.GetType().Name);

            Console.WriteLine("");

        }

        public void Withdraw(double amount)
        {

            _state.Withdraw(amount);

            Console.WriteLine("取款 {0:C} --- ", amount);

            Console.WriteLine(" 余额 = {0:C}", this.Balance);

            Console.WriteLine(" 状态 = {0}\n",

              this.State.GetType().Name);

        }


    }


}

关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
目前维护的开源产品:https://gitee.com/475660
原文地址:https://www.cnblogs.com/starcrm/p/1358561.html