📄 staterealworld.cs
字号:
using System;
using System.Windows.Forms;
using System.Text;
//状态模式(State)
//意图
// 允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。
//适用性
// 1.一个对象的行为取决于它的状态, 并且它必须在运行时刻根据状态改变它的行为。
// 2.一个操作中含有庞大的多分支的条件语句,且这些分支依赖于该对象的状态。这个状态通常用一个或多个枚举常量表示。
// 通常, 有多个操作包含这一相同的条件结构。State模式将每一个条件分支放入一个独立的类中。
// 这使得你可以根据对象自身的情况将对象的状态作为一个对象,这一对象可以不依赖于其他对象而独立变化。
namespace DesignPattern.StateRealWorld
{
class StateRealWorld : AbstractPattern
{
//银行帐号根据不同的存款额,有不同的处理方式(状态机) -- Red(欠费),Silver(普通),Gold(贵宾)
//随着存款的变动,更改当前所处的State(StateChangeCheck 函数中)
public static void Run(TextBox tbInfo)
{
s_tbInfo = tbInfo;
s_tbInfo.Text = "";
// Open a new account
Account account = new Account("Jim Johnson");
// Apply financial transactions
account.Deposit(500.0);
account.Deposit(300.0);
account.Deposit(550.0);
account.PayInterest();
account.Withdraw(2000.00);
account.Withdraw(1100.00);
// Wait for user
//Console.Read();
}
}
// "State"
abstract class State
{
protected Account account;
protected double balance;
protected double interest;
protected double lowerLimit;
protected double upperLimit;
// Properties
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();
}
// "ConcreteState"
// Account is overdrawn
class RedState : State
{
double serviceFee;
// Constructor
public RedState(State state)
{
this.balance = state.Balance;
this.account = state.Account;
Initialize();
}
private void Initialize()
{
// Should come from a datasource
interest = 0.0;
lowerLimit = -100.0;
upperLimit = 0.0;
serviceFee = 15.00;
}
public override void Deposit(double amount)
{
balance += amount;
StateChangeCheck();
}
public override void Withdraw(double amount)
{
amount = amount - serviceFee;
DesignPattern.FormMain.OutputInfo("No funds available for withdrawal!");
}
public override void PayInterest()
{
// No interest is paid
}
private void StateChangeCheck()
{
if (balance > upperLimit)
{
account.State = new SilverState(this);
}
}
}
// "ConcreteState"
// Silver is non-interest bearing state
class SilverState : State
{
// Overloaded constructors
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()
{
// Should come from a datasource
interest = 0.0;
lowerLimit = 0.0;
upperLimit = 1000.0;
}
public override void Deposit(double amount)
{
balance += amount;
StateChangeCheck();
}
public override void Withdraw(double amount)
{
balance -= amount;
StateChangeCheck();
}
public override void PayInterest()
{
balance += interest * balance;
StateChangeCheck();
}
private void StateChangeCheck()
{
if (balance < lowerLimit)
{
account.State = new RedState(this);
}
else if (balance > upperLimit)
{
account.State = new GoldState(this);
}
}
}
// "ConcreteState"
// Interest bearing state
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()
{
// Should come from a database interest = 0.05;
lowerLimit = 1000.0;
upperLimit = 10000000.0;
}
public override void Deposit(double amount)
{
balance += amount;
StateChangeCheck();
}
public override void Withdraw(double amount)
{
balance -= amount;
StateChangeCheck();
}
public override void PayInterest()
{
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);
}
}
}
// "Context"
class Account
{
private State state;
private string owner;
// Constructor
public Account(string owner)
{
// New accounts are 'Silver' by default
this.owner = owner;
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);
DesignPattern.FormMain.OutputInfo("Deposited {0:C} --- ", amount);
DesignPattern.FormMain.OutputInfo(" Balance = {0:C}", this.Balance);
DesignPattern.FormMain.OutputInfo(" Status = {0}\n" , this.State.GetType().Name);
DesignPattern.FormMain.OutputInfo("");
}
public void Withdraw(double amount)
{
state.Withdraw(amount);
DesignPattern.FormMain.OutputInfo("Withdrew {0:C} --- ", amount);
DesignPattern.FormMain.OutputInfo(" Balance = {0:C}", this.Balance);
DesignPattern.FormMain.OutputInfo(" Status = {0}\n" , this.State.GetType().Name);
}
public void PayInterest()
{
state.PayInterest();
DesignPattern.FormMain.OutputInfo("Interest Paid --- ");
DesignPattern.FormMain.OutputInfo(" Balance = {0:C}", this.Balance);
DesignPattern.FormMain.OutputInfo(" Status = {0}\n", this.State.GetType().Name);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -