G
Guest
I am trying to create a generic Command object (following the Command design
pattern) that allows me to specify a generic type, an object to operate on,
and most importantly, a public Property on that object for getting and
setting the value in question.
Currently the only approach I've found to work is to use delegates along
with Get and Set functions (instead of using a Property). See code below.
Anyone have a better approach than what I have as follows?
class Person
{
private string name = "";
private int age = 0;
public string Name { set { name = value; } get { return name; } }
public int Age { set { age = value; } get { return age; } }
// These needed to be added to get the Generic PersonCommand to work
public void SetName(string Name) { name = Name; }
public string GetName() { return name; }
}
abstract class Command
{
public abstract void Do();
public abstract void Undo();
}
class PersonCommand<T> : Command
{
private Person person = null;
private T oldValue;
private T newValue;
private SetPersonValue setPersonValue;
private GetPersonValue getPersonValue;
// These are needed because I can't work with Properties
public delegate void SetPersonValue(T value);
public delegate T GetPersonValue();
public PersonCommand(Person P, SetPersonValue setValue,
GetPersonValue getValue,
T NewValue)
{
setPersonValue = setValue;
getPersonValue = getValue;
oldValue = getPersonValue();
newValue = NewValue;
person = P;
}
public override void Do()
{
setPersonValue(newValue);
// Would prefer to do the following:
// person.??? = newValue;
}
public override void Undo()
{
setPersonValue(oldValue);
}
}
public void test()
{
Person Robert = new Person();
Robert.Name = "Robert";
Robert.Age = 30;
PersonCommand<string> name2Command = new
PersonCommand<string>(Robert, new
PersonCommand<string>.SetPersonValue(Robert.SetName),
new PersonCommand<string>.GetPersonValue(Robert.GetName),
"Bobby");
name2Command.Do();
Debug.WriteLine("Name = " + Robert.Name);
name2Command.Undo();
Debug.WriteLine("Name = " + Robert.Name);
}
pattern) that allows me to specify a generic type, an object to operate on,
and most importantly, a public Property on that object for getting and
setting the value in question.
Currently the only approach I've found to work is to use delegates along
with Get and Set functions (instead of using a Property). See code below.
Anyone have a better approach than what I have as follows?
class Person
{
private string name = "";
private int age = 0;
public string Name { set { name = value; } get { return name; } }
public int Age { set { age = value; } get { return age; } }
// These needed to be added to get the Generic PersonCommand to work
public void SetName(string Name) { name = Name; }
public string GetName() { return name; }
}
abstract class Command
{
public abstract void Do();
public abstract void Undo();
}
class PersonCommand<T> : Command
{
private Person person = null;
private T oldValue;
private T newValue;
private SetPersonValue setPersonValue;
private GetPersonValue getPersonValue;
// These are needed because I can't work with Properties
public delegate void SetPersonValue(T value);
public delegate T GetPersonValue();
public PersonCommand(Person P, SetPersonValue setValue,
GetPersonValue getValue,
T NewValue)
{
setPersonValue = setValue;
getPersonValue = getValue;
oldValue = getPersonValue();
newValue = NewValue;
person = P;
}
public override void Do()
{
setPersonValue(newValue);
// Would prefer to do the following:
// person.??? = newValue;
}
public override void Undo()
{
setPersonValue(oldValue);
}
}
public void test()
{
Person Robert = new Person();
Robert.Name = "Robert";
Robert.Age = 30;
PersonCommand<string> name2Command = new
PersonCommand<string>(Robert, new
PersonCommand<string>.SetPersonValue(Robert.SetName),
new PersonCommand<string>.GetPersonValue(Robert.GetName),
"Bobby");
name2Command.Do();
Debug.WriteLine("Name = " + Robert.Name);
name2Command.Undo();
Debug.WriteLine("Name = " + Robert.Name);
}