繁体   English   中英

C#中的命令模式与简单工厂模式

[英]command pattern vs Simple factory pattern in C#

我了解命令模式属于行为模式,而简单工厂模式则主要解决了对象创建问题。 但是,当实施时,我觉得两者都遵循相似的步骤。

public interface ICommand
{
    string Name { get; }
    void Execute();
} 
public class StartCommand : ICommand
{
    public void Execute()
    {
        Console.WriteLine("I am executing StartCommand");
    } 
    public string Name
    {
        get { return "Start"; }
    }
} 
public class StopCommand : ICommand
{
    public void Execute()
    {
        Console.WriteLine("I am executing StopCommand");
    } 
    public string Name
    {
        get { return "Stop"; }
    }
}
public class Invoker
{
    ICommand cmd = null;
    public ICommand GetCommand(string action)
    {
        switch (action)
        {
            case "Start":
                cmd = new StartCommand();
                break;
            case "Stop":
                cmd = new StopCommand();
                break;
            default:
                break;
        }
        return cmd;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Invoker invoker = new Invoker();
        // Execute Start Command
        ICommand command = invoker.GetCommand("Start");
        command.Execute();
        // Execute Stop Commad
        command = invoker.GetCommand("Stop");
        command.Execute();
        Console.WriteLine("Command Pattern demo");
        Console.Read();
    }
}

这是简单工厂模式的示例。

 interface IGet
    {
        string ConC(string s1, string s2);
    }

    class clsFirst : IGet
    {
        public string ConC(string s1, string s2)
        {
            string Final = "From First: " + s1 + " and " + s2;
            return Final;
        }
    }

    class clsSecond : IGet
    {
        public string ConC(string s1, string s2)
        {
            string Final = "From Second: " + s1 + " and " + s2;
            return Final;
        }
    } 
    class clsFactory
    {
        static public IGet CreateandReturnObj(int cChoice)
        {
            IGet ObjSelector = null;

            switch (cChoice)
            {
                case 1:
                    ObjSelector = new clsFirst();
                    break;
                case 2:
                    ObjSelector = new clsSecond();
                    break;
                default:
                    ObjSelector = new clsFirst();
                    break;
            }
            return ObjSelector;

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IGet ObjIntrface = null;
            int input = Convert.ToInt32(Console.ReadLine());
            ObjIntrface = clsFactory.CreateandReturnObj(input);
            string res = ObjIntrface.ConC("First", "Second");

        }
    }

我看不到任何实现上的差异。 有人可以帮我理解吗?

Invoker是出厂模式,但不是命令:

public interface ICommandFactory
{
    ICommand CreateCommand(string action);
}

public class Invoker : ICommandFactory
{
    public ICommand CreateCommand(string action)
    {
        ...
    }
}

命令模式是一种行为设计模式,其中的对象用于封装以后执行动作或触发事件所需的所有信息。

因此,您的StartCommandStopCommand类仅实现命令模式。


UPDATE

在第二个示例中,您在其中实现了一个简单工厂,而clsFirstclsSecond不是命令,因为它们的命名并不意味着捕获任何输入或要对其执行操作的上下文-它们只是实现相同接口的类。 您必须具有逻辑上的InvokeExecute方法,该方法隐含对某物的操作(有时您可以撤消该操作)。 但是,如果您不是人类,而是某种AI,则IGetstring ConC(string s1, string s2)完全可以是命令和操作的定义:)

再一次,工厂和命令之间没有什么共同点。 您可以使用factory创建命令,但这是可选的。 除了命令,您还可以使用factory创建任何东西。

暂无
暂无

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

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