C#设计模式——状态模式

今天我来说下状态模式。状态模式用处多多,可以实现状态的切换,废话不多说,代码如下:

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();
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值