vendredi 16 septembre 2016

command pattern vs Simple factory pattern in C#

I understood command pattern belongs to behavioral pattern and simple factory pattern mainly solves the object Creation issues. But when comes in to implementation I feel both follows similar steps.

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();
    }
}

It resembles simple factory pattern. Where it differs in the implementation?

Aucun commentaire:

Enregistrer un commentaire