CSharp设计模式读书笔记(21):状态模式(学习难度:★★★☆☆,使用频率:★★★☆☆)

模式角色与结构:CSharp设计模式读书笔记(21):状态模式(学习难度:★★★☆☆,使用频率:★★★☆☆)

示例代码:(本示例在具体状态类中实现状态切换,也可以在环境类中实现状态切换。状态模式一定程度上违背开闭原则)

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(); //
account.Withdraw(); // -1000
account.Deposit(); //
account.Withdraw(); // -2000
account.Withdraw(); // -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 = ;
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 > - && _account.Balance <= )
{
_account.State = new OverdraftState(this._account);
Console.WriteLine("OverdraftState...");
}
else if (_account.Balance == -)
{
_account.State = new RestrictedState(this._account);
Console.WriteLine("RestrictedState...");
}
else if (_account.Balance < -)
{
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 > )
{
_account.State = new NormalState(this._account);
Console.WriteLine("NormalState...");
}
else if (_account.Balance == -)
{
_account.State = new RestrictedState(this._account);
Console.WriteLine("RestrictedState...");
}
else if (_account.Balance < -)
{
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 > )
{
_account.State = new NormalState(this._account);
Console.WriteLine("NormalState...");
}
else if (_account.Balance > -)
{
_account.State = new OverdraftState(this._account);
Console.WriteLine("OverdraftState...");
}
}
}
}
上一篇:Java的OOP三大特征之一——封装


下一篇:[再寄小读者之数学篇](2014-06-26 Besov space estimates)