Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on what you're showing here it looks like the benefit of the command pattern is lost. There are a few reasons you might want to use the command pattern in the context of a WinForms app.</p> <p>You want to execute a command later</p> <pre><code>public interface ICommand { void Execute(); } </code></pre> <p>Keep a history of executed commands so they can be undone by the user</p> <pre><code>public interface ICommand { void Execute(); void Undo(); } </code></pre> <p>Check permissions to see if the current user has rights to execute the command. For example, maybe you have a <code>RefundCustomerCommand</code> and not all customer service agents have the right to issue a refund so you want to disable a button on the form.</p> <pre><code>public interface ICommand { void Execute(); bool CanExecute { get; } } </code></pre> <p>You can also roll multiple commands together in a composite like this:</p> <pre><code>public class CompositeCommand : ICommand { private readonly List&lt;ICommand&gt; commands; public CompositeCommand() { commands = new List&lt;ICommand&gt;(); } public void Add(ICommand command) { commands.Add(command); } public void Execute() { foreach (var command in commands) command.Execute(); } } </code></pre> <p>The command pattern also works nicely with the decorator. You can easily add additional cross-cutting behavior to your commands like retry logic:</p> <pre><code>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 &gt; 3) throw; Execute(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload