简体   繁体   English

C# 中 State 设计模式的概念示例

[英]Conceptual example of State Design Pattern in C#

I am currently studying the state Design Pattern and I think I pretty clear about how it works.我目前正在研究 state 设计模式,我想我很清楚它是如何工作的。 I was looking at a conceptual example which I have posted below.我正在查看我在下面发布的概念示例。 There is just one part of the code that I don't quite understand.只有一部分代码我不太明白。

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

In the concrete class Active there is property of type action delegate which is passed as a parameter in its constructor.在具体的 class Active 中有一个动作委托类型的属性,它在其构造函数中作为参数传递。 Also in the constructor of the context class a delegate of type action is passed as a parameter.此外,在上下文 class 的构造函数中,动作类型的委托作为参数传递。 Now conceptually I understand this is done as this provides a backreference to the Context object (Account),with the State ie closed or Active.现在从概念上讲,我理解这是完成的,因为这提供了对上下文 object(帐户)的反向引用,其中 State 即关闭或活动。 This backreference can be used by States to transition the Context to another State.状态可以使用此反向引用将上下文转换到另一个 State。 My first question is why a delegate was used?我的第一个问题是为什么使用委托?

However I was trying to get the code to run and debug to illustrate what was happening.但是,我试图让代码运行和调试以说明正在发生的事情。 Therefore my question is in the code how do I need initialize a new Account?因此我的问题是在代码中我需要如何初始化一个新帐户? Accounts begin with a NotVerified state.帐户以 NotVerified state 开头。 I tried this code.我试过这段代码。

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

If I then want to determine the state of the account how do I code that?如果我想确定帐户的 state 我该如何编码?

I managed to get the State details by making the State attribute in the Account class public.我设法通过公开帐户 class 中的 State 属性来获取 State 详细信息。 Then in the program class I could execute the following:然后在程序 class 中,我可以执行以下操作:

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

I also changed the Account constructor to take and initial amount for the balance.我还更改了 Account 构造函数以获取余额的初始金额。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM