简体   繁体   English

Command模式或Strategy模式更适合客户端 - 服务器调用吗?

[英]Is the Command pattern or the Strategy pattern more appropriate for client-server calls?

I have a need to create a client-server program during a job interview, in which the client sends commands and the server processes them. 我需要在求职面试中创建客户端 - 服务器程序,其中客户端发送命令并且服务器处理它们。

The server needs to be able to be able to change easily, meaning that there could be more command types later on. 服务器需要能够轻松更改,这意味着以后可能会有更多的命令类型。

So I implemented it using Strategy, meaning that the class processing commands looked similar to: 所以我使用Strategy实现它,这意味着类处理命令看起来类似于:

public class ServerProcessor
{
    Dictionary<string, Action<string>> commandsType;

    public ServerProcessor()
    {
        commandsType = new Dictionary<string, Action<string>>();
    }

    public void RegisterCommand(string commandName, Action<string> command)
    {
        commandsType.Add(commandName, command);
    }

    public void Process(string commandName, string vars)
    {
        if (commandsType.ContainsKey(commandName))
        {
            commandsType[commandName].Invoke(vars);
        }
    }
}

After I did that the interviewer said I needed to implement it using the Command pattern, but did not say why. 在我这样做之后,面试官说我需要使用Command模式实现它,但没有说明原因。

The Command pattern will be something like this: Command模式将是这样的:

public interface ICommand
{
    void Execute(string vars);
    string GetName();
}

public class ServerProcessor2
{
    List<ICommand> commandsType;

    public ServerProcessor2()
    {
        commandsType = new List<ICommand>();
    }

    public void RegisterCommand(ICommand commandName)
    {
        commandsType.Add(commandName);
    }

    public void Process(string commandName, string vars)
    {
        foreach (ICommand item in commandsType)
        {
            string name = item.GetName();

            if (name.Equals(commandName))
            {
                item.Execute(vars);
            } 
        }
    }
}

is there a reason why the Command pattern is better in this scenario, or is it just the interviewer's point of view? 在这种情况下,Command模式更好,还是仅仅是面试官的观点?

Your two examples are very similar. 你的两个例子非常相似。 They both use the Strategy pattern, not Command. 他们都使用策略模式,而不是命令。

A Strategy encapsulates a process which has input of a given kind and output of another given kind. 策略封装了一个过程,该过程具有给定种类的输入和另一种给定种类的输出。 Usually the choice of Strategy depends on the input. 通常,策略的选择取决于输入。

The Command pattern represents operations on the system as objects which are easy to construct, transmit and persist, so you can attach them to UI elements, queue them, log them, use them as the basis for undo, etc. Command模式将系统上的操作表示为易于构造,传输和持久化的对象,因此您可以将它们附加到UI元素,对它们进行排队,记录它们,将它们用作撤消的基础等。

Your examples both take a string with the command name and another with parameters. 您的示例都使用带有命令名称的字符串和带参数的另一个字符串。 The Command pattern would use a different class (with a common superclass) for each command, and include the parameters as properties of the object. Command模式将为每个命令使用不同的类(具有公共超类),并将参数包括为对象的属性。 Your examples both use Strategy to execute each command; 您的示例都使用策略来执行每个命令; in the first example your Strategies are Actions and in your second the Strategies are (somewhat misleadingly) ICommands. 在第一个例子中,你的策略是动作,在你的第二个例子中,策略是(有点误导性)ICommands。 Your examples are really only different in their naming and in that one stores its Strategies in a Dictionary and the other in a List. 您的示例实际上只是在命名方面有所不同,并且其中一个将其策略存储在字典中而另一个存储在列表中。

Now to actually answer your question: There is nothing in the problem description that requires the use of either pattern. 现在实际回答你的问题:问题描述中没有任何内容需要使用任何一种模式。 Command is a fine pattern for client-server interfaces, because it enables the things I mentioned above, and Strategy is a fine pattern for executing Commands, so using both would be a good choice. 命令是客户端 - 服务器接口的一个很好的模式,因为它支持我上面提到的东西,而Strategy是执行命令的一个很好的模式,所以使用它们将是一个不错的选择。 Neither is required, however, so as far as we can tell Command is just your interviewer's choice (probably because they want to know whether you know it). 然而,两者都不是必需的,所以我们可以告诉Command只是你的面试官的选择(可能是因为他们想知道你是否知道它)。

One benefit of using Command pattern in this scenario can be to enable "undoing" some of the actions. 在此方案中使用命令模式的一个好处是可以启用“撤消”某些操作。 If your ICommand interface is implemented as: 如果您的ICommand接口实现为:

public interface ICommand
{
    void Execute(string vars);
    void Undo();
    string GetName();
}

When you create conrete implementations of this interface, adding undo logic for each type of command is easy. 当您创建此接口的conrete实现时,为每种类型的命令添加撤消逻辑很容易。 Eg if the command adds some text to a document, the undo action removes that text. 例如,如果命令向文档添加了一些文本,则撤消操作将删除该文本。 The context of the command is saved within the command object. 命令的上下文保存在命令对象中。

The server can add the concrete command objects to a stack after execution. 服务器可以在执行后将具体命令对象添加到堆栈。 In case an action needs to be undone, it's a simple matter of calling code similar to: 如果需要撤消操作,则调用类似于以下内容的代码是一件简单的事情:

executedCommandsStack.pop().undo();

The Command processor pattern is oriented towards a server's handling of long-running commands. Command处理器模式面向服务器处理长时间运行的命令。 This is definitely Command and not Strategy, as it's a variant of GoF Command . 这绝对是Command而不是Strategy,因为它是GoF Command的变种。

Was the interview Android-oriented? 面试是面向Android吗?

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

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