博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
State pattern(模拟一个借记卡的例子)
阅读量:4135 次
发布时间:2019-05-25

本文共 4547 字,大约阅读时间需要 15 分钟。

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);

        }

 

    }

}

转载地址:http://sspvi.baihongyu.com/

你可能感兴趣的文章
B - Number Puzzle ZOJ - 2836(容斥原理 数学)
查看>>
E - Olympic Medal CodeForces - 215B(数学 思维)
查看>>
codeforces1073D. Berland Fair
查看>>
codeforces 1073B. Vasya and Books
查看>>
Queue at the School CodeForces - 266B
查看>>
A - Average distance HDU - 2376(树形)
查看>>
B - Adding Digits CodeForces - 260A
查看>>
Party at Hali-Bula POJ - 3342(树形dp)
查看>>
E - Balls and Boxes CodeForces - 260C(思维)
查看>>
A - Anniversary party HDU - 1520(没有上司的舞会)
查看>>
B - Greg's Workout CodeForces - 255A(思维)
查看>>
E - Code Parsing CodeForces - 255B(思维)
查看>>
D - Undoubtedly Lucky Numbers CodeForces - 244B(数论 )
查看>>
Minimal coverage(贪心 区间覆盖)
查看>>
201709-5 除法 ccf(树状数组)
查看>>
little w and Segment Coverage(差分)
查看>>
Weak Pair HDU - 5877(dfs+树状数组+离散化+二分)
查看>>
Codeforces Round #572 (Div. 2)(ABCD1D2E)
查看>>
Query on a tree HDU - 3804(线段树求区间最大+树链剖分)
查看>>
Doom HDU - 5239(线段树+思维)
查看>>