473,795 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2563
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 GetCurrentSelec tion() within the command processor class.
The more sophisticated solution would be to use an interface for this
task (e.g. IHostSelectionS ervice) which is handed to the constructor of
the CommandProcesso r 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
IHostSelectioSe rvice 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
6475
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, but we want to rename them so we want to implement the Adapter classes in such a way that we only have to rename the Adaptee classes. We prefer to use #define's because of the better run-time performance, in stead of implementing wrapper...
2
3601
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 solution. I am implementing the Mediator pattern to create an event-driven app. The Collaborators (which watch for various events and which all have a reference to the Mediator object) will call handleEvent(Event e) on the Mediator object when...
1
4661
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 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...
1
5559
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 runs as a service, I would want it to call ServiceBase.Run (which will then call the execution logic). If it is run at the command-line, I simply want it to run as a normal application.
4
2881
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 for you. This one is pretty straight forward. In the Command Pattern an object encapsulates everything needed to execute a method in another object. http://www.fluffycat.com/SDCMSv2/PHP-Design-Patterns-Command/
4
3334
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, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
2
3398
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 I'd like the attention of fp as well as C++ gurus if available. The language I'm implementing - PILS - is dynamically
13
5042
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 implement operator new/delete I can't see a way to indicate, on delete, how many objects must be destroyed (or how big the space is) - alternatively I can't figure out what are the alignment requirements so that the implementation, after calling my...
4
2119
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 prediction is ready, then there is a "WeatherViewer" which calls methods from the "Observer Interface".
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10436
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.