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.