Avatar of CipherIS
CipherIS
Flag 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
C#.NET Programming

Avatar of undefined
Last Comment
Jeevan Bordoloi

8/22/2022 - Mon
Kyle Abrahams

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.
Daniel Van Der Werken

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?
kaufmed

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Jeevan Bordoloi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.