欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

23种设计模式-19-状态者模式

程序员文章站 2022-06-13 13:54:50
...

用户:

/// <summary>
    /// 用户
    /// </summary>
    public class Person
    {
        public Person()
        {
            this.State = new NewState(0, this);
        }

        public State State { get; set; }

        /// <summary>
        /// 存钱
        /// </summary>
        /// <param name="money"></param>
        public void SaveMoney(decimal money)
        {
            this.State.SaveMoney(money);
            Show();
        }

        /// <summary>
        /// 取钱
        /// </summary>
        /// <param name="money"></param>
        public void GetMoney(decimal money)
        {
            this.State.GetMoney(money);
            Show();
        }

        /// <summary>
        /// 查看获得的利息
        /// </summary>
        public void ShowInterest()
        {
            this.State.ShowInterest();
            Show();
        }

        public void Show()
        {
            Console.WriteLine("当前余额:{0}", CoreService.MoneyFormat(this.State.Money));
            Console.WriteLine("当前利率:{0}", this.State.Rate);
            Console.WriteLine("当前状态:{0}", this.State.GetType().Name);
        }

 

状态抽象类:

public abstract class State
    {
        /// <summary>
        /// 用户
        /// </summary>
        public Person Person { get; set; }

        /// <summary>
        /// 余额
        /// </summary>
        public decimal Money { get; set; }

        /// <summary>
        /// 利率
        /// </summary>
        public decimal Rate { get; set; }

        /// <summary>
        /// 下限
        /// </summary>
        public decimal Min { get; set; }

        /// <summary>
        /// 上限
        /// </summary>
        public decimal Max { get; set; }

        /// <summary>
        /// 存钱
        /// </summary>
        /// <param name="money"></param>
        public abstract void SaveMoney(decimal money);

        /// <summary>
        /// 取钱
        /// </summary>
        /// <param name="money"></param>
        public abstract void GetMoney(decimal money);

        /// <summary>
        /// 查看获得的利息
        /// </summary>
        public abstract void ShowInterest();

        public abstract void ValidateStateChange();

三种状态:

1.

/// <summary>
    /// 新开户
    /// </summary>
    public class NewState : State
    {
        public NewState(decimal money, Person person)
        {
            this.Money = money;
            this.Person = person;
            this.Rate = Convert.ToDecimal(0.002);
            this.Min = 0;
            this.Max = 1000;
        }

        public NewState(State state) : this(state.Money, state.Person)
        {

        }

        public override void SaveMoney(decimal money)
        {
            this.Money += money;
            ValidateStateChange();
        }

        public override void GetMoney(decimal money)
        {
            this.Money -= money;
            ValidateStateChange();
        }

        public override void ShowInterest()
        {
            Console.WriteLine("您是新开账户,当前可以获得的利息:{0}", CoreService.MoneyFormat(this.Money * this.Rate));
        }

        public override void ValidateStateChange()
        {
            if (this.Money < this.Min)
            {
                this.Person.State = new BadState(this);
            }
            else if (this.Money > this.Max)
            {
                this.Person.State = new SuperState(this);
            }
        }
    }

2.

/// <summary>
    /// 透支账户
    /// </summary>
    public class BadState : State
    {
        public BadState(State state)
        {
            this.Money = state.Money;
            this.Person = state.Person;
            this.Rate = Convert.ToDecimal(0.005);
            this.Min = Decimal.MinValue;
            this.Max = 0;
        }

        public override void SaveMoney(decimal money)
        {
            this.Money += money;
            ValidateStateChange();
        }

        public override void GetMoney(decimal money)
        {
            Console.WriteLine("您已透支{0}元,无法取钱", CoreService.MoneyFormat(this.Money));
        }

        public override void ShowInterest()
        {
            Console.WriteLine("您的账户已透支,您没有利息可以获得");
        }

        public override void ValidateStateChange()
        {
            if (this.Money >= this.Max)
            {
                this.Person.State = new NewState(this);
            }
        }
    }

3.

/// <summary>
    /// 高级账户
    /// </summary>
    public class SuperState : State
    {
        public SuperState(State state)
        {
            this.Money = state.Money;
            this.Person = state.Person;
            this.Rate = Convert.ToDecimal(0.005);
            this.Min = 1000;
            this.Max = Decimal.MaxValue;
        }

        public override void SaveMoney(decimal money)
        {
            this.Money += money;
            ValidateStateChange();
        }

        public override void GetMoney(decimal money)
        {
            this.Money -= money;
            ValidateStateChange();
        }

        public override void ShowInterest()
        {
            Console.WriteLine("您是高级账户,当前可以获得的利息:{0}", CoreService.MoneyFormat(this.Money * this.Rate));
        }

        public override void ValidateStateChange()
        {
            if (this.Money <= this.Min)
            {
                this.Person.State = new NewState(this);
            }
        }
    }

测试:

class Program
    {
        /// <summary>
        /// 状态者模式:
        /// 一个对象有多种不同的状态来表现出不同的形态
        /// 如果不停的用if else去判断的话,会导致程序的耦合性很高,而且不利于维护
        /// 所以此时拥有了状态者模式
        ///
        /// 此文中的例子:
        /// 用户的银行账户:
        /// 分为三个状态:
        /// 1.新开户:账户金额为0-1000,利息为0.2%
        /// 2.高级账户:账户余额为10000以上,利息为0.5%
        /// 3.透支账户:账户余额为0以下,利息为0
        /// 
        /// 状态公共的操作有:存钱,取钱,查询可以获得的利息费用
        /// 用户的操作有:存钱,取钱,查询可以获得的利息费用
        /// 用户的属性有:当前余额,当前利率
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var person = new Person();
            person.Show();
            Console.WriteLine("-------------------------------------------------");
            person.GetMoney(500);
            Console.WriteLine("-------------------------------------------------");
            person.GetMoney(100);
            Console.WriteLine("-------------------------------------------------");
            person.SaveMoney(888);
            Console.WriteLine("-------------------------------------------------");
            person.ShowInterest();
            Console.WriteLine("-------------------------------------------------");
            person.SaveMoney(20000);
            Console.WriteLine("-------------------------------------------------");
            person.ShowInterest();
            Console.WriteLine("-------------------------------------------------");
            Console.ReadKey();
        }
    }

23种设计模式-19-状态者模式