寻找UI的Command模式的例子 [英] Looking for example of Command pattern for UI

查看:157
本文介绍了寻找UI的Command模式的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个WinForm .Net应用程序,其基本UI包括工具栏按钮,菜单项和击键,所有这些都启动相同的底层代码。现在,每个这些事件处理程序调用一个常用的方法来执行这个功能。



从我读过的这种类型的动作可以由命令设计模式,具有自动启用/禁用或取消选中UI元素的附加优点。 / p>

我一直在搜索一个好的示例项目,但真的没有找到一个。有没有人可以分享一个很好的例子?

解决方案

让我们首先确定我们知道命令模式是什么: p>


命令模式将请求
封装为一个对象,并给它一个已知的
公共接口。命令模式
确保每个对象都收到其
自己的命令,并在发送方和接收方之间提供一个解耦
。发件人
是一个调用
操作的对象,接收方是一个收到该请求的对象
,并且对
执行。


这是一个例子。有很多方法可以做到这一点,但是我将采用接口基础方法,使代码更具可测试性。我不知道你喜欢什么语言,但我正在C#中写这个。



首先,创建一个描述命令的界面。

  public interface ICommand 
{
void Execute();
}

其次,创建将实现命令界面的命令对象。

  public class CutCommand:ICommand 
{
public void Execute()
{
//当您调用CutCommand.Execute方法时,放置您要执行的代码。
}
}

第三,我们需要设置我们的调用者或发件人对象

  public class TextOperations 
{
public void Invoke(ICommand命令)
{
command.Execute();
}
}

第四,创建将使用调用者的客户端对象/ sender对象。

  public class Client 
{
static void Main()
{
TextOperations textOperations = new TextOperations();
textOperation.Invoke(new CutCommand());
}
}

我希望你能把这个例子放到用于您正在处理的应用程序。如果您想要更多的澄清,只需让我知道。


I'm working on a WinForm .Net application with the basic UI that includes toolbar buttons, menu items and keystrokes that all initiate the same underlying code. Right now the event handlers for each of these call a common method to perform the function.

From what I've read this type of action could be handled by the Command design pattern with the additional benefit of automatically enabling/disabling or checking/unchecking the UI elements.

I've been searching the net for a good example project, but really haven't found one. Does anyone have a good example that can be shared?

解决方案

Let's first make sure we know what the Command pattern is:

Command pattern encapsulates a request as an object and gives it a known public interface. Command Pattern ensures that every object receives its own commands and provides a decoupling between sender and receiver. A sender is an object that invokes an operation, and a receiver is an object that receives the request and acts on it.

Here's an example for you. There are many ways you can do this, but I am going to take an interface base approach to make the code more testable for you. I am not sure what language you prefer, but I am writing this in C#.

First, create an interface that describes a Command.

public interface ICommand
{
    void Execute();
}

Second, create command objects that will implement the command interface.

public class CutCommand : ICommand
{
    public void Execute()
    {
        // Put code you like to execute when the CutCommand.Execute method is called.
    }
}

Third, we need to setup our invoker or sender object.

public class TextOperations
{
    public void Invoke(ICommand command)
    {
        command.Execute();
    }
}

Fourth, create the client object that will use the invoker/sender object.

public class Client
{
    static void Main()
    {
        TextOperations textOperations = new TextOperations();
        textOperation.Invoke(new CutCommand());
    }
}

I hope you can take this example and put it into use for the application you are working on. If you would like more clarification, just let me know.

这篇关于寻找UI的Command模式的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆