Link to home
Create AccountLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

C# Command Design Patter Class Question

I am following the code in the below example.  For the most part it fits my needs.

http://www.dotnet-tricks.com/Tutorial/designpatterns/23JE110913-Command-Design-Pattern---C

All of my commands need to execute.  However two commands actually needs to return different values.  Is there a way to modify the pattern so I can return values for these two commands but leave the other commands executing as is?

Thanks
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

If I understand you correctly you may want to do an integer or an enum type of value . . . then you can set that in the execute and return whatever value you needed.

If you showed your code and what you were trying to do it would be helpful.
There are probably a couple of ways to do this -- if I understand what you want to do correctly.

Option 1: Create an object, you populate this object when the command executes, and then populate part of the object that's appropriate to the information you want returned and return the whole object.

Option 2: Return a Tuple<type1, type2> where you are, again, populating the type appropriately.

On the receiving end, you'll need to know what you're looking for. So, example:

public class MyInfoObject
{
     public  int MyNumberOfThingiesDone;
     public string MyReallyCoolInfo;
}

Open in new window

then
public interface ICommand
{
      MyInfoClass Execute();
}

Open in new window

Something like:
public class FlipDownCommand : ICommand
{
 private Light _light;
 
 public FlipDownCommand(Light light)
 {
 _light = light;
 }
 
 public MyInfoClass Execute()
 {
     MyInfoClass infoClass = new MyInfoClass();
     infoClass.MyNumberOfThingiesDone = 101;
     infoClass.MyReallyCoolInfo = string.Empty;
     _light.TurnOff();
     return infoClass;
 }
}

Open in new window

public class FlipUpCommand : ICommand
{
 private Light _light;
 
 public FlipUpCommand(Light light)
 {
 _light = light;
 }
 
 public MyInfoClass Execute()
 {
     MyInfoClass infoClass = new MyInfoClass();
     infoClass.MyNumberOfThingiesDone = int.MinValue;
     infoClass.MyReallyCoolInfo = "Kittens are cute."
     _light.TurnOn();
     return infoClass;
 }
}

Open in new window


And also:
public class Switch
{
 private List<ICommand> _commands = new List<ICommand>();
 
 public MyInfoClass StoreAndExecute(ICommand command)
 {
      _commands.Add(command);
      return command.Execute();
 }
}

Open in new window

then in use:
 if (cmd == "ON")
 {
   string infoINeed = s.StoreAndExecute(switchUp);
 }
 else if (cmd == "OFF")
 {
   int InfoNumbers = s.StoreAndExecute(switchDown);
 }
 else
 {
 Console.WriteLine("Command \"ON\" or \"OFF\" is required.");
 }

Open in new window


The Tuple implementation would be similar.

Does this help?
While Tuple would work functionally, semantically you would be doing a disservice to other developers reading your code. With a Tuple, you get properties such as Item1, Item2, Item3, etc. There's no meaning behind those names. The meaning is in the value itself. I would lean more towards the custom class, or possibly a generic implementation of your command classes.
ASKER CERTIFIED SOLUTION
Avatar of Jeevan Bordoloi
Jeevan Bordoloi
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account