473,799 Members | 3,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ICo mmand command)
{
if (command is NameChange)
this.RedoComman d(command as NameChange);
else
command.Redo();
}
private void RedoCommand(Nam eChange command)
{
command.fNameBe fore = this.fName;
this.fName = command.fNameAf ter;
}
}
/*************** *************** ***************/

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 1574
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.RedoC ommand(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.RedoHan dler = subject.NameCha nge;

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

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
3617
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, first and foremost. If it works well, even better. The same goes for ease of maintenance, memory footprint, speed, etc, etc. Most of the time, people are writing code for a use in the *real world*, and not just as an academic exercise. Look at...
3
1371
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 deterred people from small methods" (followed by the basic "Extract Method" advice). In Skip Montanaro's "Python Performance Tips" (http://manatee.mojam.com/~skip/python/fastpython.html) I read: ". . . use local variables wherever possible. If the...
12
15533
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 call in a method (and bailing out immediately if it is true). However, if this is such a good idea, why is it not common industry practice? Mohan
4
3230
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 that I could externalize my JavaScript, but that will not be practical throughout this application. Is there any way to get around this issue? Xalan processor. Stripped down stylesheet below along with XHTML output. <?xml version='1.0'?>...
28
15395
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 (frame_size,num_blocks,num_frames,frame_locator) size_t frame_size; unsigned short num_blocks; unsigned short num_frames; Kl_frame_locator *locator; { /* code goes here */
7
1846
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 choose to change data providers). Therefore i thought to myself, a type of Adapter Pattern is best here and so i proceeded with that. here's an example of what i did (note this implementation differs from the text book one due to the way data...
13
3941
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 file...whatever. I described my situation to the guy I'm doing work for and he did some research for me and came up with the following link. http://www.novapdf.com/ The part that should excite us Access developers is their SDK
24
1812
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 of precondition constraints)? Like: def foo(a, b, c, d): assert type(a) == str assert type(b) == str assert type(c) == int assert type(d) == bool
0
9687
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
9543
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,...
1
10237
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
10029
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
9077
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
7567
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
6808
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();...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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.