using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DesignPattern
{
internal class StatePattern
{
}
//状态模式
//允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类
#region 应用实例
//银行卡账户状态 普通状态 透支状态 受限状态
//银行账户 充当环境类
class Account
{
private AccountState state; //维持一个对抽象状态的引用
private string owner; //开户名
private double balance = 0;//账户余额
public Account(string owner, double init)
{
this.owner = owner;
this.balance = init;
this.state = new NormalState(this);//设置初始状态
Console.WriteLine("{0}开户,初始金额为{1}", this.owner, init);
Console.WriteLine("------------------------------------");
}
public double Balance
{
get { return balance; }
set { balance = value; }
}
public void SetState(AccountState state)//setter方法注入状态
{
this.state = state;
}
public void Deposit(double amount)
{
Console.WriteLine("{0}存款{1}", this.owner, amount);
state.Deposit(amount);//调用状态对象下的Deposit()方法
Console.WriteLine("现在余额为{0}", this.Balance);
Console.WriteLine("现在账户状态为{0}", this.state.GetType().ToString());
Console.WriteLine("---------------------------------------------");
}
public void Withdraw(double amount)
{
Console.WriteLine("{0}取款{1}", this.owner, amount);
state.WithDraw(amount);//调用状态对象的withdraw方法
Console.WriteLine("现在余额为{0}", this.Balance);
Console.WriteLine("现在账户状态为{0}", this.state.GetType().ToString());
Console.WriteLine("---------------------------------------------");
}
public void ComputeInterset()
{
state.ComputeInterest();//调用状态对象的ComputeInterest()方法
}
}
//账户状态类,充当抽象状态类
abstract class AccountState
{
private Account acc;
public Account Acc
{
get { return acc; }
set { acc = value; }
}
public abstract void Deposit(double amount);
public abstract void WithDraw(double amount);
public abstract void ComputeInterest();
public abstract void StateCheck();
}
//正常状态类:充当具体状态类
class NormalState : AccountState
{
public NormalState(Account acc)
{
this.Acc = acc;
}
public NormalState(AccountState state)
{
this.Acc = state.Acc;
}
public override void ComputeInterest()
{
Console.WriteLine("正常状态,无需支付利息");
}
//存款
public override void Deposit(double amount)
{
Acc.Balance = Acc.Balance + amount;
StateCheck();
}
//状态检查
public override void StateCheck()
{
if (Acc.Balance > -2000 && Acc.Balance < 0)
{
Acc.SetState(new OverdraftState(this));
}
else if (Acc.Balance == -2000)
{
Acc.SetState(new RestrictedState(this));
}
else if (Acc.Balance < -2000)
{
Console.WriteLine("操作受限!");
}
}
//取款
public override void WithDraw(double amount)
{
Acc.Balance = Acc.Balance - amount;
StateCheck();
}
}
//透支状态类 充当具体状态类
class OverdraftState : AccountState
{
public OverdraftState(AccountState state)
{
this.Acc = state.Acc;
}
public override void ComputeInterest()
{
Console.WriteLine("正在计算利息!");
}
public override void Deposit(double amount)
{
Acc.Balance = Acc.Balance + amount;
StateCheck();
}
public override void StateCheck()
{
if (Acc.Balance > 0)
{
Acc.SetState(new NormalState(this));
}
else if (Acc.Balance == -2000)
{
Acc.SetState(new RestrictedState(this));
}
else if (Acc.Balance < -2000)
{
Console.WriteLine("操作受限!");
}
}
public override void WithDraw(double amount)
{
Acc.Balance = Acc.Balance - amount;
StateCheck();
}
}
//受限状态类 充当具体状态类
class RestrictedState : AccountState
{
public RestrictedState(AccountState state)
{
this.Acc = state.Acc;
}
public override void ComputeInterest()
{
Console.WriteLine("正在计算利息!");
}
public override void Deposit(double amount)
{
Acc.Balance = Acc.Balance + amount;
StateCheck();
}
public override void StateCheck()
{
if (Acc.Balance > 0)
{
Acc.SetState(new NormalState(this));
}
else if (Acc.Balance > -2000)
{
Acc.SetState(new OverdraftState(this));
}
}
public override void WithDraw(double amount)
{
Console.WriteLine("账号受限,取款失败!");
}
}
//客户端测试
class ProgramTest1
{
public void main()
{
Account acc = new Account("段誉", 0.0);
acc.Deposit(1000);
acc.Withdraw(2000);
acc.Deposit(3000);
acc.Withdraw(4000);
acc.Withdraw(1000);
acc.ComputeInterset();
Console.Read();
//输出结果
//段誉开户,初始金额为0
//---------------------------------
/*
段誉存坤1000
现在余额为1000
现在账户状态为
*/
}
}
#endregion
}