1

I'm trying to create a load of "commands" - basically a string defining the command name, and about 4 functions related to it. Normally, I'd bundle each "command" into a static class and just have lots of different static classes for each command.

However, I also want to be able to have a list of all the commands available, so I have the ability to call the functions by knowing the command name - this can't be done with static classes.

I thought about using reflection to build a list. I also thought about using a singleton pattern and in each constructor, add the single object to a static list shared by all "command" classes. Neither of these methods is particularly neat though...

Is there a design pattern for this scenario? Or any methods people have used in the past? All advice appreciated!

I'm using C#, although it's not really that relevant to the question.

1
  • You could try using Microsoft MEF - it was made for exactly such purposes and IMO it already provides in some way the functionality you want to achieve. Commented Aug 23, 2014 at 21:12

2 Answers 2

1

You've outlined the two best methods.

The registry pattern is where you have a class (the Registry) that has a list of all the available Commands. The benefit is that it is simple, the downside is that the developer needs to know that the command needs to be registered. Use a base class to help guide the developer (who will be writing more commands) in what to do.

The pluggable pattern is where you use reflection to find all the loaded types in the Current AppDomain. This has the benefit that it can be done after the application has loaded, giving you the option of loading assemblies dynamically.

To help keep your code maintainable, I would recommend doing the simplest thing that works while guiding the developer (who will be making new commands) what to do.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know that the way I do it is modeled after a particular pattern, but I have a program used in production that has uses a Dictionary and a interface. The interface is simple enough

public interface KeyboardActionCommand
{
    void ExecuteOnKeyDown();
    void ExecuteOnKeyUp();
}

To make a list of predefined commands was easy in that case with a Dictionary<string, KeyboardActionCommand> The draw back to that was if in my case when I eventually had to add customization such as when I made a new class called OpenProgramKeyboardActionAcommand as is inferred by the name it opened a program, but it was not practical to make a bunch of predefined versions of that class. I instead had to make a class to help build those types of commands. I did have a few in my Dictionary though. example i availableCommands.Add("Open Calculator", new OpenProgramKeyboardActionCommand(@"C:\Windows\System32\calc.exe"));

so it does have a draw back.

another idea is to make a delegate for the value part of the dictionary. Then you can make your static class that has a bunch of implemented delegate methods. That one isn't too bad either.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.