C# 如何正确实现状态模式? 一个帐户可以有多种状态,如冻结、活动、未验证、关闭 并且帐户可以执行以下操作:存款()、取款()、HoldServerified()、关闭()、冻结() 这些操作的实施可能因帐户的当前状态而异
下面是我处理上述情况的方法。 但是,如果我们遇到必须: 在账户当前状态被冻结时进行存款时,扣除10%的存款? 账户C# 如何正确实现状态模式? 一个帐户可以有多种状态,如冻结、活动、未验证、关闭 并且帐户可以执行以下操作:存款()、取款()、HoldServerified()、关闭()、冻结() 这些操作的实施可能因帐户的当前状态而异,c#,.net,oop,design-patterns,object-oriented-analysis,C#,.net,Oop,Design Patterns,Object Oriented Analysis,下面是我处理上述情况的方法。 但是,如果我们遇到必须: 在账户当前状态被冻结时进行存款时,扣除10%的存款? 账户 class Account { public decimal Balance { get; private set; } private IAccountState State { get; set; } public Account(Action onUnfreeze) {
class Account
{
public decimal Balance { get; private set; }
private IAccountState State { get; set; }
public Account(Action onUnfreeze)
{
this.State = new NotVerified(onUnfreeze);
}
public void Deposit(decimal amount)
{
this.State = this.State.Deposit(() => { this.Balance += amount; });
}
public void Withdraw(decimal amount)
{
this.State = this.State.Withdraw(() => { this.Balance -= amount; });
}
public void HolderVerified()
{
this.State = this.State.HolderVerified();
}
public void Close()
{
this.State = this.State.Close();
}
public void Freeze()
{
this.State = this.State.Freeze();
}
}
IAccountState
interface IAccountState
{
IAccountState Deposit(Action addToBalance);
IAccountState Withdraw(Action substractFromBalance);
IAccountState Freeze();
IAccountState HolderVerified();
IAccountState Close();
}
IAccountState的具体实施
interface IAccountState
{
IAccountState Deposit(Action addToBalance);
IAccountState Withdraw(Action substractFromBalance);
IAccountState Freeze();
IAccountState HolderVerified();
IAccountState Close();
}
活动的
class Active : IAccountState
{
private Action OnUnfreeze { get; }
public Active(Action onUnfreeze)
{
OnUnfreeze = onUnfreeze;
}
public IAccountState Deposit(Action addToBalance)
{
addToBalance();
return this;
}
public IAccountState Withdraw(Action substractFromBalance)
{
substractFromBalance();
return this;
}
public IAccountState HolderVerified() => this;
public IAccountState Freeze() => new Frozen(this.OnUnfreeze);
public IAccountState Close() => new Closed();
}
未验证
class NotVerified : IAccountState
{
public Action OnUnfreeze { get; }
public NotVerified(Action onUnfreeze)
{
this.OnUnfreeze = onUnfreeze;
}
public IAccountState Close() => new Closed();
public IAccountState Deposit(Action addToBalance)
{
addToBalance();
return this;
}
public IAccountState Freeze() => this;
public IAccountState HolderVerified() => new Active(this.OnUnfreeze);
public IAccountState Withdraw(Action substractFromBalance) => this;
}
当账户当前状态被冻结时,您必须扣除10%的存款,您将如何处理这种情况?
我不知道如何修改NotVerified类中的存款方法以满足要求:
public IAccountState Deposit(Action addToBalance)
{
addToBalance();
return this;
}
我将使用枚举而不是接口:
enum AccountState
{
NotVerified,
Active,
Frozen,
Closed
}
class Account
{
public decimal Balance { get; private set; }
// I assumed the bank would want to deposit the 10% to its own account
public decimal Fees { get; set; }
public AccountState state {get; private set; } = AccountState.NotVerified;
public Account() {}
public void Deposit(decimal amount) {
if (this.state == AccountState.Active)
this.Balance += amount;
else if (this.state == AccountState.Frozen)
{
this.Balance += amount * (decimal).9;
this.Fees += amount * (decimal).1;
}
}
public void Withdraw(decimal amount) {
if (this.state == AccountState.Active)
this.Balance -= amount;
}
public bool HolderVerified() {
return (this.state == AccountState.Active);
}
public void Close() {
this.state = AccountState.Closed;
}
public void Freeze() {
this.state = AccountState.Frozen;
}
}