标签:
用户需求:
定义一个俱乐部,它的顾客有三个状态:访客(VistorState),会员(MemberState),贵宾(VIPState)。每个顾客都有一个帐户(ClubAccount),帐户上存放顾客的存款,根据存款的不同数量切换会员的级别,其中 0~100 为访客,100~1000为会员,1000 以上为贵宾。当顾客需要俱乐部的服务时,俱乐部将根据顾客的当前级别提供与其级别对应的服务内容。当顾客的存款发生变化时,需要及时对他们的状态进行改变。
相关UML:
具体代码如下:
/// <summary> /// 环境类 /// </summary> public class Account { public AccountState State { set; get; } public string Owner { set; get; } public double Balance { set; get; } public Account(string owner, double initialAmount) { Owner = owner; Balance = initialAmount; State = new VistorState(this); //设置初始状态 } public void SetBalance(double amount) { Balance = amount; } //存款 public void Deposit(double amount) { Console.WriteLine("存入" + amount); State.Deposit(amount); Console.WriteLine("用户状态变为" + State._Statename); } //消费 public void Shopping(double amount) { Console.WriteLine("消费" + amount); State.Shopping(amount); Console.WriteLine("用户状态变为" + State._Statename); } }
/// <summary> /// 抽象状态类 /// </summary> public abstract class AccountState { protected Account _account; public string _Statename; public abstract void Deposit(double amount); public abstract void Shopping(double amount); public abstract void StateCheck(); }
/// <summary> /// 访客状态类 /// </summary> public class VistorState:AccountState { public VistorState(Account account) { _account = account; _Statename = "访客账户"; StateCheck(); //默认的检查 } //取款 public override void Deposit(double amount) { _account.Balance += amount; Console.WriteLine(_account.Owner + " 向账户中存款" + amount + "元,存款后的账户余额为:" + _account.Balance); StateCheck(); } //消费 public override void Shopping(double amount) { double newBalance = _account.Balance - amount; if (newBalance > 0) { _account.Balance -= amount; Console.WriteLine(_account.Owner + "在《访客服务》中消费" + amount + "元,账户余额:" + _account.Balance); } else { Console.WriteLine("您的账户余额不足,请充值。"); } StateCheck(); } //检查用户状态 public override void StateCheck() { if (_account.Balance > 100 && _account.Balance < 1000) { //会员用户状态 _account.State = new MemberState(_account); } else if (_account.Balance > 1000) { //贵宾用户状态 _account.State = new VIPState(_account); } } }
/// <summary> /// 会员状态类 /// </summary> public class MemberState:AccountState { public MemberState(Account account) { _account = account; _Statename = "会员账户"; } //取款 public override void Deposit(double amount) { _account.Balance += amount; Console.WriteLine(_account.Owner + " 向账户中存款" + amount + "元,存款后的账户余额为:" + _account.Balance); StateCheck(); } //消费 public override void Shopping(double amount) { double newBalance = _account.Balance - amount; if (newBalance > 0) { _account.Balance -= amount; Console.WriteLine(_account.Owner + "在《会员服务》中消费" + amount + "元,账户余额:" + _account.Balance); } else { Console.WriteLine("您的账户余额不足,请充值。"); } StateCheck(); } //检查用户状态 public override void StateCheck() { if (_account.Balance > 0 && _account.Balance < 100) { //访客用户状态 _account.State = new VistorState(_account); } else if (_account.Balance > 1000) { //贵宾用户状态 _account.State = new VIPState(_account); } } }
/// <summary> /// 贵宾状态类 /// </summary> public class VIPState:AccountState { public VIPState(Account account) { _account = account; _Statename = "贵宾账户"; } //取款 public override void Deposit(double amount) { _account.Balance += amount; Console.WriteLine(_account.Owner + " 向账户中存款" + amount + "元,存款后的账户余额为:" + _account.Balance); StateCheck(); } //消费 public override void Shopping(double amount) { double newBalance = _account.Balance - amount; if (newBalance > 0) { _account.Balance -= amount; Console.WriteLine(_account.Owner + "在《贵宾服务》中消费" + amount + "元,账户余额:" + _account.Balance); } else { Console.WriteLine("您的账户余额不足,请充值。"); } StateCheck(); } //检查用户状态 public override void StateCheck() { if (_account.Balance > 0 && _account.Balance < 100) { //访客用户状态 _account.State = new VistorState(_account); } else if (_account.Balance > 100&&_account.Balance<1000) { //会员用户状态 _account.State = new MemberState(_account); } } }
/// <summary> /// 客户端代码 /// </summary> class Program { static void Main(string[] args) { Account account = new Account("王大锤", 99); Console.WriteLine("开户成功。姓名:" + account.Owner + " 初始金额:" + account.Balance + " 账户类型:" + account.State._Statename); Console.WriteLine("----------------------------"); account.Deposit(20); Console.WriteLine("-----------------------------"); account.Shopping(30); Console.WriteLine("-----------------------------"); account.Deposit(9999); Console.WriteLine("------------------------------"); Console.ReadLine(); } }
运行结果如下:
标签:
原文地址:http://www.cnblogs.com/zjl931223/p/5097126.html