Question

I'm working on a WinForm .Net application with the basic UI that includes toolbar buttons, menu items and keystrokes that all initiate the same underlying code. Right now the event handlers for each of these call a common method to perform the function.

From what I've read this type of action could be handled by the Command design pattern with the additional benefit of automatically enabling/disabling or checking/unchecking the UI elements.

I've been searching the net for a good example project, but really haven't found one. Does anyone have a good example that can be shared?

Was it helpful?

Solution

Let's first make sure we know what the Command pattern is:

Command pattern encapsulates a request as an object and gives it a known public interface. Command Pattern ensures that every object receives its own commands and provides a decoupling between sender and receiver. A sender is an object that invokes an operation, and a receiver is an object that receives the request and acts on it.

Here's an example for you. There are many ways you can do this, but I am going to take an interface base approach to make the code more testable for you. I am not sure what language you prefer, but I am writing this in C#.

First, create an interface that describes a Command.

public interface ICommand
{
    void Execute();
}

Second, create command objects that will implement the command interface.

public class CutCommand : ICommand
{
    public void Execute()
    {
        // Put code you like to execute when the CutCommand.Execute method is called.
    }
}

Third, we need to setup our invoker or sender object.

public class TextOperations
{
    public void Invoke(ICommand command)
    {
        command.Execute();
    }
}

Fourth, create the client object that will use the invoker/sender object.

public class Client
{
    static void Main()
    {
        TextOperations textOperations = new TextOperations();
        textOperation.Invoke(new CutCommand());
    }
}

I hope you can take this example and put it into use for the application you are working on. If you would like more clarification, just let me know.

OTHER TIPS

Your on the right track. Basically you will have a model that represents the document. You will use this model in the CutCommand. You will want to change the CutCommand's constructor to accept the information you want to cut. Then everytime, say the Cut Button is clicked, you invoke a new CutCommand and passing the arguments in the constructor. Then use those arguments in the class when the Execute method is called.

Try open source, .NET editors like SharpDevelop or Notepad++.

There is (naturally) some discussion of the Command Pattern at http://c2.com/cgi/wiki?CommandPattern that might be helpful.

Qt uses Command Pattern for Menubar/Toolbar items.

QActions are created seperately from QMenuItem and QToolbar, and the Actions can be assigned to QMenuItem and QToolbar with setAction() and addAction() method respectively.

http://web.archive.org/web/20100801023349/http://cartan.cas.suffolk.edu/oopdocbook/html/menus.html

http://web.archive.org/web/20100729211835/http://cartan.cas.suffolk.edu/oopdocbook/html/actions.html

I can't help you with example link, but can provide example by myself.

1) Define ICommand interface:

public interface ICommand {
   void Do();
   void Undo();
}

2) Do your ICommand implementations for concrete commands, but also define abstract base class for them:

public abstract class WinFormCommand : ICommand {

}

3) Create command invoker:

public interface ICommandInvoker {
  void Invoke(ICommand command);
  void ReDo();
  void UnDo();
}

public interface ICommandDirector {
  void Enable(ICommand);
  void Disable(ICommand);
}

public class WinFormsCommandInvoker : ICommandInvoker, ICommandDirector {

    private readonly Dictionary<ICommand, bool> _commands;
    private readonly Queue<ICommand> _commandsQueue;       
    private readonly IButtonDirector _buttonDirector;

    // you can define additional queue for support of ReDo operation

    public WinFormsCommandInvoker(ICommandsBuilder builder, IButtonDirector buttonDirector) {
      _commands = builder.Build();
      _buttonDirector = buttonDirector;
      _commandsQueue = new Queue<ICommand>();
    } 

    public void Invoke(ICommand command) {
        command.Do();
        __commandsQueue.Enqueue(command);
    }

    public void ReDo() {
      //you can implement this using additional queue
    }

    public void UnDo() {
      var command = __commandsQueue.Dequeue();
      command.Undo();
    }

    public void Enable(ICommand command) {
      _commands.[command] = true;
      _buttonDirector.Enable(command);
    }

    public void Disable(ICommand command) {
      _commands.[command] = false;
      _buttonDirector.Disable(command); 
    }
}

4) Now you can implement your ICommandsBuilder, IButtonDirector and add other interfaces such as ICheckBoxDirector to WinFormsCommandInvoker.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top