GUI作为一个有限状态机作为一个、状态机、GUI

2023-09-03 10:12:40 作者:胡大本事@

要实现应用程序的GUI,我想所有的逻辑去从一种形式到另一个集中。该GUI管理器将表现为一个有限状态机。虽然我觉得我已经看到了这种实现的地方,我无法找到这样的解决方案相匹配的设计模式。

To implement application's GUI I would like to have all the logic to go from one form to another centralized. This GUI manager will behave as a finite state machine. Although I think I have seen this kind of implementation somewhere, I can't find a design pattern that matches with this kind of solution.

一个表单将是这样的:

public class Login : Form
{
    ...

    private void EnterButton_Click()
    {
        ...

        string user = loginTextBox.Text;
        string password = passwordTextBox.Text;
        bool isValid = SecurityManager.CheckUserLogin(user,password);

        GUIManager.FormEnd(FormLogin,new LoginData(user, pass, isValid));
    }

    ...
}

和GUI的经理会做这样的事情:

And the GUI manager will do something like this:

public class GUIManager
{
    ...

    public void FormEnd(FormType type, FormData data)
    {
        switch (type)
        {
            ...
            case FormLogin:
                LoginData ld = (LoginData)data;
                if (ld.Valid)
                {
                    m_loginForm.Hide();
                    m_mainForm.Show();
                }
            ...
        }
    }

    ...
}

到达这一点我有以下问题:是否有正式确定这个想法德兴模式?如果有,是否.NET某种程度上支持呢?如果没有,它听起来像一个良好的实施主意吗?谢谢!

Reaching this point I have the following questions: is there a desing pattern that formalizes this idea? If there is, does .NET support it somehow? If there isn't, does it sound like a good implementation idea? Thanks!

推荐答案

这是一个伟大的想法!如此之大,事实上,它已经做过,可能是在可扩展的应用程序开发中使用的最常见的模式(思如Visual Studio,Eclipse和喜欢的IDE)。

It's a great idea! So great, in fact, that it's been done before and is probably the most common pattern used in extensible application development (think of IDEs like Visual Studio, Eclipse and the like).

一个例子, SCSF(它利用CAB),从MS模式与实践小组,使用此模式即开即用现成的,构建在两个WinForms和WPF可插拔和可扩展的复合应用程序。它使用的实际模式涉及施工分层状态机称为控制用例和流经该应用程序的工作项。我会考虑的模式与实践家伙是怎么做之前,我实现它作为我自己的心血结晶。我用它在许多场合,它是值得的。

One example, SCSF (which leverages CAB), from the MS Patterns and Practices Group, uses this pattern out-of-the-box to construct pluggable and extensible composite applications in both WinForms and WPF. The actual pattern it uses involves construction of hierarchical state machines called WorkItems that control usecases and flow through the application. I'd look into how the Patterns and Practices guys did it before I implement it as my own brainchild. I've used it on many occasions and it's well worth it.