Wannabe template command object or real thing?

The command object template is one that I still could not really understand, and I found an implementation in the code that I am currently working in, so I studied it for a long time, and it was difficult for me to understand if I can finally get it real world example. The problem is that I'm sure this is not implemented correctly, and this is just an attempt by someone who just read about it and thought it made sense here.

Let me show you this (for privacy reasons this will be greatly simplified, but I will do my best to show the basic concepts):

public class CommandOne
{
   public CommandOne(Manager manager, MyForm form)
   {
      m_manager = manager;
      m_form = form;
   }

   public void Execute()
   {
       m_manager.CommandOne(m_form);
   }
}

public class CommandTwo
{
   public CommandTwo(Manager manager, MyForm form)
   {
      m_manager = manager;
      m_form = form;
   }

   public void Execute()
   {
       m_manager.CommandTwo(m_form);
   }
}

The first thing that strikes me as strange is that these two classes do not inherit from any abstract class and do not implement a common interface.

, , :

public class MyForm : System.Windows.Forms.Form
{
   public MyForm(Manager manager)
   {
      m_manager = manager;
   }

   private void SomeMethod()
   {
      ....
      var cmd = new CommandOne(manager, this);
      cmd.Execute();
      ...
   }

   private void OtherMethod()
   {
      ....
      var cmd = new CommandTwo(manager, this);
      cmd.Execute();
      ...
   }
}

, , , , . , "", , , , .

- , , , , , , , , ?

.

+5
1

, , , . , WinForms.

public interface ICommand
{
    void Execute();
}

,

public interface ICommand
{
    void Execute();

    void Undo();
}

, , . , , RefundCustomerCommand, , .

public interface ICommand
{
    void Execute();

    bool CanExecute { get; }
}

:

public class CompositeCommand : ICommand
{
    private readonly List<ICommand> commands;

    public CompositeCommand()
    {
        commands = new List<ICommand>();
    }

    public void Add(ICommand command)
    {
        commands.Add(command);
    }

    public void Execute()
    {
        foreach (var command in commands) command.Execute();
    }
}

. , :

public class RetryOnTimeout : ICommand
{
    private readonly ICommand command;
    private int numberOfRetries;

    public RetryOnTimeout(ICommand command, int numberOfRetries)
    {
        this.command = command;
        this.numberOfRetries = numberOfRetries;
    }

    public void Execute()
    {
        try
        {
            command.Execute();
        }
        catch (TimeoutException)
        {
            if (++numberOfRetries > 3)
                throw;

            Execute();
        }
    }
}
+9
source

All Articles