【问题标题】:Conceptual example of State Design Pattern in C#C# 中状态设计模式的概念示例
【发布时间】:2020-05-30 12:07:41
【问题描述】:

我目前正在研究状态设计模式,我想我很清楚它是如何工作的。我正在查看我在下面发布的概念示例。只有一部分代码我不太明白。

using System;

namespace BranchingDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.ReadLine();
        }


    }
    // State Interface
    interface IAccountState
    {
        IAccountState Deposit(Action addToBalance);
        IAccountState Withdraw(Action subtractFromBalance);
        IAccountState Freeze();
        IAccountState HolderVerified();
        IAccountState Close();
    }

    //Context
    class Account
    {
        public decimal Balance { get; private set; }


        private IAccountState State { get; set; }

        public Account(Action onUnfreeze)
        {
            this.State = new NotVerified(onUnfreeze);
        }

        public void Deposit(decimal amount)
        {
            this.State = this.State.Deposit(() => { this.Balance += amount; });
        }

        public void Withdraw(decimal amount)
        {
            this.State = this.State.Withdraw(() => { this.Balance -= amount; });
        }

        public void HolderVerified()
        {
            this.State = this.State.HolderVerified();
        }

        public void Close()
        {
            this.State = this.State.Close();
        }

        public void Freeze()
        {
            this.State = this.State.Freeze();
        }

    }
    //Concrete State
    class Active : IAccountState
    {
        private Action OnUnfreeze { get; }

        public Active(Action onUnfreeze)
        {
            this.OnUnfreeze = onUnfreeze;
        }

        public IAccountState Deposit(Action addToBalance)
        {
            addToBalance();
            return this;
        }

        public IAccountState Withdraw(Action subtractFromBalance)
        {
            subtractFromBalance();
            return this;
        }

        public IAccountState Freeze() => new Frozen(this.OnUnfreeze);
        public IAccountState HolderVerified() => this;
        public IAccountState Close() => new Closed();
    }

    //Concrete State

    class Closed : IAccountState
    {
        public IAccountState Deposit(Action addToBalance) => this;
        public IAccountState Withdraw(Action subtractFromBalance) => this;
        public IAccountState Freeze() => this;
        public IAccountState HolderVerified() => this;
        public IAccountState Close() => this;
    }
}

在具体类 Active 中,有一个动作委托类型的属性,该属性在其构造函数中作为参数传递。同样在上下文类的构造函数中,动作类型的委托作为参数传递。现在从概念上讲,我理解这是完成的,因为这提供了对上下文对象(帐户)的反向引用,状态为关闭或活动。 State 可以使用此反向引用将 Context 转换为另一个 State。 我的第一个问题是为什么要使用委托?

但是我试图让代码运行和调试以说明正在发生的事情。因此我的问题是在代码中我需要如何初始化一个新帐户?帐户以 NotVerified 状态开始。我试过这段代码。

Action initialState = () => { };
            Account account= new Account(initialState);

如果我想确定帐户的状态,我该如何编码?

【问题讨论】:

    标签: c# oop design-patterns delegates


    【解决方案1】:

    我设法通过公开 Account 类中的 State 属性来获取 State 详细信息。然后在程序类中我可以执行以下操作:

    Action initialState = () => { };
    Account testAccount= new Account(initialState,0);
    var currentState = testAccount.State;
    Console.WriteLine(currentState.GetType().Name);
    testAccount.HolderVerified();
    currentState = testAccount.State;
    Console.WriteLine(currentState.GetType().Name);
    testAccount.Deposit(12);
    currentState = testAccount.State;
    Console.WriteLine(currentState.GetType().Name + $" Balance: {testAccount.Balance}");
    testAccount.Close();
    currentState = testAccount.State;
    Console.WriteLine(currentState.GetType().Name);
    Console.ReadLine();
    

    我还更改了 Account 构造函数以获取余额的初始金额。

    【讨论】:

      猜你喜欢
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多