473,395 Members | 1,474 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,395 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 2520
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.