模式角色与结构:
示例代码:(本示例在具体状态类中实现状态切换,也可以在环境类中实现状态切换。状态模式一定程度上违背开闭原则)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.StatePattern { class Program { static void Main(string[] args) { Account account = new Account(0.0); account.Deposit(1000); // 1000 account.Withdraw(2000); // -1000 account.Deposit(3000); // 2000 account.Withdraw(4000); // -2000 account.Withdraw(1000); // -3000 account.ComputeInterest(); Console.ReadLine(); } } // 环境类 class Account { public Account(double balance) { this._balance = balance; this._state = new NormalState(this); } public void Deposit(double amount) { _state.Deposit(amount); } public void Withdraw(double amount) { _state.Withdraw(amount); } public void ComputeInterest() { _state.ComputeInterest(); } public double Balance { get { return _balance; } set { _balance = value; } } internal AccountState State { get { return _state; } set { _state = value; } } private double _balance = 0; private AccountState _state; } // 抽象状态类 abstract class AccountState { protected Account _account; public virtual void Deposit(double amount) { _account.Balance += amount; StateCheck(); } public virtual void Withdraw(double amount) { _account.Balance -= amount; StateCheck(); } public abstract void ComputeInterest(); public abstract void StateCheck(); } // 具体状态类 class NormalState : AccountState { public NormalState(Account account) { this._account = account; } public override void ComputeInterest() { Console.WriteLine("No interest..."); } // 状态转换 public override void StateCheck() { if (_account.Balance > -2000 && _account.Balance <= 0) { _account.State = new OverdraftState(this._account); Console.WriteLine("OverdraftState..."); } else if (_account.Balance == -2000) { _account.State = new RestrictedState(this._account); Console.WriteLine("RestrictedState..."); } else if (_account.Balance < -2000) { Console.WriteLine("Forbidden..."); } } } // 具体状态类 class OverdraftState : AccountState { public OverdraftState(Account account) { this._account = account; } public override void ComputeInterest() { Console.WriteLine("Compute interest..."); } // 状态转换 public override void StateCheck() { if (_account.Balance > 0) { _account.State = new NormalState(this._account); Console.WriteLine("NormalState..."); } else if (_account.Balance == -2000) { _account.State = new RestrictedState(this._account); Console.WriteLine("RestrictedState..."); } else if (_account.Balance < -2000) { Console.WriteLine("Forbidden..."); } } } // 具体状态类 class RestrictedState : AccountState { public RestrictedState(Account account) { this._account = account; } public override void Withdraw(double amount) { Console.WriteLine("Account is restricted, withdraw failed..."); } public override void ComputeInterest() { Console.WriteLine("Compute interest..."); } // 状态转换 public override void StateCheck() { if (_account.Balance > 0) { _account.State = new NormalState(this._account); Console.WriteLine("NormalState..."); } else if (_account.Balance > -2000) { _account.State = new OverdraftState(this._account); Console.WriteLine("OverdraftState..."); } } } }