2016-09-17 52 views
0

我理解命令模式属于行为模式,简单工厂模式主要解决对象创建问题。但是当实现时我感觉两者都遵循类似的步骤。命令模式vs 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"); 

     } 
    } 

我看不出有什么区别。有人能帮助我理解这一点吗?

+2

事实上,你使用工厂模式来创建命令模式的命令并没有使另一个类似... –

回答

4

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

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

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

命令模式是在其中一个对象被用于封装要执行的动作或触发事件所需的所有信息的行为设计图案在以后的时间

所以你的StartCommandStopCommand类只实现命令模式。


UPDATE

在第二个例子,在这里实现了一个简单的工厂,clsFirstclsSecond不是命令,因为它们的命名并不意味着任何输入的捕获或上下文来执行一个动作 - 它们只是实现相同接口的类。你必须有一个合乎逻辑的InvokeExecute方法,这个方法暗示对某事采取行动(有时你可以撤销该行动)。但是如果你不是人,但是某种人工智能,IGetstring ConC(string s1, string s2)完全可以定义为你的命令和动作:)

工厂和工厂之间没有什么共同之处一个命令。您可以使用工厂创建命令,但它是可选的。除了命令之外,你可以使用工厂创建任何东西。

+1

感谢您的回答。我想详细了解这一点。我已经更新了简单工厂模式的示例。你可以分享你的想法,我怎样才能区分两者? –