473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Implementing the Command pattern

TEK
Hello

I'm wondering if anyone out there might give some
input/suggestions/viewpoints around the Command pattern.
In my case, the number one priority for using the pattern is undo support.
Some now, a lot more in the future.
I think I have a pretty good understanding about how the command pattern is
tought to work, however during implementation I'm htting a couple of quite
large design considerations that I'm not sure I have the answare to.

There is problably no 100% answares to the issues, but maybe some best
practices?

The main issues I'm looking at is:

1) Should the command class or the receiver implement the code that peforms
the operations the command is for.

2) Should the command class be given all arguments needed to peform the
operations when initialized, or should it be collected during execution.

3) Should the command class be allowed to peform UI operations, as asking
the user for confirmation, or should it be limited to peform pure business
operations.

Any feedback would be appriciated...

Regards, TEK

Jul 21 '05 #1
1 2516
TEK wrote:
I'm wondering if anyone out there might give some
input/suggestions/viewpoints around the Command pattern.
In my case, the number one priority for using the pattern is undo
support. Some now, a lot more in the future.
I think I have a pretty good understanding about how the command
pattern is tought to work, however during implementation I'm htting a
couple of quite large design considerations that I'm not sure I have
the answare to.

There is problably no 100% answares to the issues, but maybe some best
practices?

The main issues I'm looking at is:

1) Should the command class or the receiver implement the code that
peforms the operations the command is for.
If your implemenatation of the command pattern is straightforward the
answer is: The command class itself.

On the other hand you might have encountered the same problems as I
have so the answer could be: The command processor. See

http://www.vico.org/pages/PatronsDis...d%20Processor/

for details of this pattern.
2) Should the command class be given all arguments needed to peform
the operations when initialized, or should it be collected during
execution.
When you hand the arguments to the constructor of the command class
this implies that you cannot store references to commands since they
are created every time they are executed. This complicates the binding
of commands to toolbar or menu items.

To simplify the binding I would suggest to collect the arguments during
execution.

Normally these arguments would be something one could express as some
kind of selection. To get the current selection one could implement a
method like GetCurrentSelection() within the command processor class.
The more sophisticated solution would be to use an interface for this
task (e.g. IHostSelectionService) which is handed to the constructor of
the CommandProcessor class.

To allow undo and redo of commands the commands must be enabled to
return appropriate data structures. These data structures should be
handled as opaque; only the command who created it can process it. The
pattern to implement that kind of data structures is called "Memento".
See

http://www.vico.org/pages/PatronsDis...ern%20Memento/

for details.

For redo and undo support the command class could have two methods,
Undo() and Redo() which accept a reference to this data structure.

The command processor is responsible for storing the undo/redo stack
and therfore to call the undo/redo methods of the commands.
3) Should the command class be allowed to peform UI operations, as
asking the user for confirmation, or should it be limited to peform
pure business operations.


That depends on the complexity of you software :)

To allow UI operations within the commands itself you greatly simplify
the process of writing commands but you make your commands more
dependant on your UI (which makes refatoring of the UI more
complicated).

If you are using something like the above mentioned
IHostSelectioService you should extremly reduce the need of UI
interaction. So the only UI interaction I can think of are more generic
like confimations or file open dialogs.

All in all I would recommend to allow MessageBox() and the standard
Windows Forms dialogs but nothing else.

--
Immo Landwerth - Visual Studio 2003 - C# - XanaNews 1.16.3.1
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Maurice | last post by:
Hi, We are implementing some wrappers in C++ according to the Adapter Pattern. The classes and their methods in the Adaptee classes (open-source library) have already the interface that we like,...
2
by: cppaddict | last post by:
I have a design question which I am posting here because the implementation will be in C++ and I think there may be C++ specific language constructs (eg, friends) that might be relevant to the...
1
by: TEK | last post by:
Hello I'm wondering if anyone out there might give some input/suggestions/viewpoints around the Command pattern. In my case, the number one priority for using the pattern is undo support. Some...
1
by: Peter Rilling | last post by:
I have an EXE that I would like to be able to run from either the command-line or as a windows service. Is there a way that I can tell which context the program is running in? Basically, if it...
4
by: FluffyCat | last post by:
New on November 29, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Command Pattern. Since you all enjoyed the Visitor Pattern so much yesterday, today I have the Command Pattern...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
2
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
4
by: Mohamed Mansour | last post by:
Hello, What is the purpose of implementing the Observer Pattern if we can trigger an event easily? For example (from books), You have a "Forecaster" which notifies "Observable" when a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.