您的位置:首页 > 编程语言 > C#

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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息