C#设计模式——状态模式
2016-12-05 11:15
239 查看
今天我来说下状态模式。状态模式用处多多,可以实现状态的切换,废话不多说,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StateModel { public class Deficit:BeginState { public Deficit(BeginState state) { this.Account = state.Account; this.LeastMoeny = state.LeastMoeny; double HighLine = 0.00; double LowLine = -100.00; double interest = 0.00; } private void checke() { if (LeastMoeny > HighLine) { Account.state = new ZeroState(this); } } public override void SaveMoney(double amount) { LeastMoeny += amount; checke(); } public override void DrawMoney(double amount) { if (LeastMoeny-amount <LowLine) { Console.WriteLine("您已经没有钱可以取了!"); } } public override void GetInterest() { } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StateModel { public class GetBenefit:BeginState { public GetBenefit(ZeroState state) { this.Account = state.Account; this.LeastMoeny = state.LeastMoeny; double HighLine = 1000000.00; double LowLine = 500.00; double interest = 0.03; } private void checke() { if (LeastMoeny < LowLine) { Account.state = new ZeroState(this); } else if (LeastMoeny < 0.0) { Account.state = new Deficit(this); } } public override void SaveMoney(double amount) { LeastMoeny += amount; checke(); } public override void DrawMoney(double amount) { LeastMoeny -= amount; checke(); } public override void GetInterest() { LeastMoeny += LeastMoeny * interest; checke(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StateModel { public class ZeroState:BeginState { public ZeroState(Account Account, double LeastMoeny) { this.Account=Account; this.LeastMoeny = LeastMoeny; double HighLine=500.00; double LowLine=0.00; double interest = 0.00; } public ZeroState(BeginState state):this(state.Account,state.LeastMoeny) { }//用于简化运算,提高效率,将状态传递到下一步 private void Checke()//定义一个用于判断目前状态的函数 { if (LeastMoeny > HighLine) { Account.state = new GetBenefit(this);//大于上限,则跳转至下一状态 } else if (LeastMoeny < LowLine) { Account.state = new Deficit(this); } } public override void SaveMoney(double amount) { LeastMoeny += amount; Checke(); } public override void DrawMoney(double amount) { LeastMoeny -= amount; Checke(); } public override void GetInterest() { LeastMoeny += LeastMoeny * interest; Checke(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StateModel { public class Account { public BeginState state{set;get; } public string AccountName { set; get; }//封装属性 public Account(string AccountName) { this.AccountName = AccountName; this.state = new ZeroState(this,0);//定义初始的变量和启动方法 } public double LeastMoney { get { return state.LeastMoeny; }//每执行一次,返回当前余额 } public void save(double amount) { state.SaveMoney(amount); Console.WriteLine("存款金额为:"+amount); Console.WriteLine("当前存款余额为:" + this.LeastMoney); Console.WriteLine("账户状态:" + this.state.GetType().Name); } public void draw(double amount) { state.DrawMoney(amount); Console.WriteLine("取款金额为:" + amount); Console.WriteLine("当前存款余额为:" + this.LeastMoney); Console.WriteLine("账户状态:" + this.state.GetType().Name); } public void interest() { state.GetInterest(); Console.WriteLine("当前获得的利息:"); Console.WriteLine("当前存款余额为:" + this.LeastMoney); Console.WriteLine("账户状态:" + this.state.GetType().Name); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StateModel { ///前面定义了三种银行账户的存储状态 public abstract class BeginState { public Account Account {set;get; } public double LeastMoeny { set; get; }//定义账户余额 public double HighLine { set; get; }//定义上限 public double LowLine { set; get; }//定义下限 public double interest{set;get; }//定义利息 public abstract void SaveMoney(double amount);//定义存钱的抽象方法 public abstract void DrawMoney(double amount);//定义取钱的抽象方法 public abstract void GetInterest();//定义获得利息的抽象方法 } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StateModel { class Program { static void Main(string[] args) { Account name = new Account("陈翔"); name.save(500.00); name.save(900.00); name.save(1800.00); name.draw(1900.00); name.draw(1400.00); name.draw(1400.00); Console.ReadKey(); } } }
相关文章推荐
- [用设计模式锻炼C#系列]之Strategy
- [用设计模式锻炼C#系列]之Decorator
- C#设计模式(2)
- C#设计模式(6)-Abstract Factory Pattern
- C#设计模式(5)-Factory Method Pattern
- 从C#的Singleton设计模式实现看.NET Framework特性对开发者的重要性
- 设计模式之C#实现(一)--AbstractFactory
- 设计模式C#描述——工厂方法模式 mutou23 [原作]
- C#设计模式学习笔记---简单工厂模式
- C#设计模式(7)-Singleton Pattern
- 设计模式之C#实现(一)--AbstractFactory(补)
- Singleton设计模式的C#实现(下)
- 设计模式之C#实现(四)---- ProtoType
- C#设计模式(3)
- C#设计模式之组合(Composite)
- [用设计模式锻炼C#系列]之Builder
- 设计模式之C#实现(三)FactoryMethod
- C#设计模式之原型(ProtoType)
- C#设计模式(3)
- C#设计模式(8)-Builder Pattern