473,396 Members | 1,995 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,396 software developers and data experts.

good practice tips needed: types & method arguments

For a hobby project, I'm discovering the "undo/redo"- and "Command
objects"- design patterns. In my specific project, I want:

1. The command objects to just hold the data; and
2. The receiving/executing objects to perform the action.

Now please consider the following code:

/************ Receiving/executing object code snippet
****************/
interface ICommand { ... }
class NameChange : ICommand { ... }

internal class myObject :
{
public virtual void RedoCommand(ICommand command)
{
if (command is NameChange)
this.RedoCommand(command as NameChange);
else
command.Redo();
}
private void RedoCommand(NameChange command)
{
command.fNameBefore = this.fName;
this.fName = command.fNameAfter;
}
}
/*********************************************/

The problem I'm looking at is as follows. Whenever I create a new
command I need to add two things:

1. Another "if" block to the generic 'ExecuteCommand' for the
ICommand.
2. A specific 'RedoCommand' for the particular new command type.

Is there any way to set this up so that the 'generic' method doesn't
need the ugly/clobbering "if/else" structure?

Note that -although I've used some of it- I'm fairly new to the "<T>"/
generics feature of .NET 2.

Any help would be much appreciated.

Jul 29 '07 #1
1 1558
Is there any way to set this up so that the 'generic' method doesn't
need the ugly/clobbering "if/else" structure?

Yes, but it doesn't involve generics. The way is to abandon your initially
stated goal of having the command object only hold the data.

Instead, make a new class for each command that holds the needed context
data and references to whatever other objects are necessary. A common
design goes along these lines:

interface ICommand
{
...
}

class SetNameCommand : ICommand
{
...
}

It's also not uncommon for the command object to simply delegate back to the
target object, calling a private member function to actually do the work (in
the example above, it's a property setter, but it could be arbitrarily more
complex).

HTH
-cd

Thanks a lot for the respons, it sheds some more light on this design
pattern.

I think though I may have followed your suggestion already. The
Command object from my first post in fact in my project is the actual
invoker, and delegates the (re)do/undo actions to the subject as you
proposed.

The solution for my own problem now also comes to me, thought I'd
share it here as well. I have a class called "CommandManager" that
combines the "Command" and "myObject" in the first place. It tells the
"Command" object which method to delegate (re)do/undo to:
"myObject.RedoCommand(ICommand x)" in the example in my first post. It
is at that point where I should already distinguish, and assign a more
specific method. In some fast-written (pseudo)code:

/*******************************************/
class CommandManager
{
// Method to activate and 'do' the command. Later the 'undo' and
'redo'
// of this commandmanager may re-invoke the command.
public void ActivateCommand(ICommand command, ICommandSubject
subject)
{
// ...

if (command is NameChange)
command.RedoHandler = subject.NameChange;

// ...
}
}
/*******************************************/

Again, thanks for the response.

-Jeroen

Jul 31 '07 #2

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

Similar topics

24
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works,...
3
by: Charles Hartman | last post by:
I know the answer to this is going to be "It depends . . .", but I want to get my mind right. In Fowler's *Refactoring* I read: "Older languages carried an overhead in subroutine calls, which...
12
by: Generic Usenet Account | last post by:
I am going through some legacy code that has an "isNull()" method defined on certain classes. I can see that this can be a good way to eliminate certain types of crashes, by making this the first...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
28
by: Michael B. | last post by:
I tend to use rather descriptive names for parameters, so the old style of declaration appeals to me, as I can keep a declaration within 80 chars: void * newKlElem...
7
by: farseer | last post by:
Here is the scenario: I have an interface which defines get methods for data that will make up a row in a table. However, the source of this data may, over time, switch/change (The company may...
13
by: salad | last post by:
Hi Guys: I was stuck. I needed to send a report to a file. My beautiful report(s) in Access were going to require loss of formatting with RTFs, a PITA in WordMailMerge, sending it as a text...
24
by: asincero | last post by:
Would it be considered good form to begin every method or function with a bunch of asserts checking to see if the parameters are of the correct type (in addition to seeing if they meet other kinds...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.