状态模式
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Account account = new Account("Learning Hard");
account.Deposit(1000.0);
account.Deposit(200.0);
account.Deposit(600.0);
account.PayInterest();
account.Withdraw(2000.00);
account.Withdraw(500.00);
Console.ReadKey();
}
}
public class Account
{
public string Owner { get; set; }
public State State { get; set; }
public double Balance { get; set; }
public Account(string owner)
{
Owner = owner;
Balance = 0.00;
State = new SilverState(this);
}
public void Deposit(double amount)
{
State.Deposit(amount);
Console.WriteLine("存款金额为 {0:C}——", amount);
Console.WriteLine("账户余额为 =:{0:C}", this.Balance);
Console.WriteLine("账户状态为: {0}", this.State.GetType().Name);
Console.WriteLine();
}
public void Withdraw(double amount)
{
State.Withdraw(amount);
Console.WriteLine("取款金额为 {0:C}——", amount);
Console.WriteLine("账户余额为 =:{0:C}", this.Balance);
Console.WriteLine("账户状态为: {0}", this.State.GetType().Name);
Console.WriteLine();
}
public void PayInterest()
{
State.PayInterest();
Console.WriteLine("Interest Paid --- ");
Console.WriteLine("账户余额为 =:{0:C}", this.Balance);
Console.WriteLine("账户状态为: {0}", this.State.GetType().Name);
Console.WriteLine();
}
}
public abstract class State
{
protected double Interest { get; set; }
protected double LowerLimit { get; set; }
protected double UpperLimit { get; set; }
public Account Account { get; set; }
public abstract void Deposit(double amount);
public abstract void Withdraw(double amount);
public abstract void PayInterest();
public abstract void StateChangeCheck();
}
public class RedState : State
{
public RedState(Account account)
{
Account = account;
Interest = 0.00;
LowerLimit = -100.0;
UpperLimit = 0.0;
}
public override void Deposit(double amount)
{
Account.Balance += amount;
StateChangeCheck();
}
public override void Withdraw(double amount)
{
Console.WriteLine("改账户为透支状态,无法取钱");
}
public override void PayInterest()
{
Console.WriteLine("改账户为透支状态,没有利息");
}
public override void StateChangeCheck()
{
if (Account.Balance > UpperLimit)
{
Account.State = new SilverState(Account);
}
}
}
public class SilverState : State
{
public SilverState(Account account)
{
Account = account;
Interest = 0.00;
LowerLimit = 0.00;
UpperLimit = 1000.00;
}
public override void Deposit(double amount)
{
Account.Balance += amount;
StateChangeCheck();
}
public override void PayInterest()
{
Account.Balance += Interest * Account.Balance;
StateChangeCheck();
}
public override void StateChangeCheck()
{
if (Account.Balance < LowerLimit)
{
Account.State = new RedState(Account);
}
else if (Account.Balance > UpperLimit)
{
Account.State = new GoldState(Account);
}
}
public override void Withdraw(double amount)
{
Account.Balance -= amount;
StateChangeCheck();
}
}
public class GoldState : State
{
public GoldState(Account account)
{
Account = account;
Interest = 0.05;
LowerLimit = 1000.00;
UpperLimit = 1000000.00;
}
public override void Deposit(double amount)
{
Account.Balance += amount;
StateChangeCheck();
}
public override void PayInterest()
{
Account.Balance += Interest * Account.Balance;
StateChangeCheck();
}
public override void StateChangeCheck()
{
if (Account.Balance < 0.0)
{
Account.State = new RedState(Account);
}
else if (Account.Balance < LowerLimit)
{
Account.State = new SilverState(Account);
}
}
public override void Withdraw(double amount)
{
Account.Balance -= amount;
StateChangeCheck();
}
}
}

浙公网安备 33010602011771号