How to decouple a Mode (normally expressed by enums) from its implementation in commands and their relationship?
Is their a good pattern describing the loose binding between a mode switch (int, enum, string, …) and its command calls? I want to add modes via config, so this must be (dynamically) easy extendable (without programming). I already know the command pattern (ICommand in C#/.Net). It could be an Dictionary of Commands and their related mode number, but what about the switching logic?
How to decouple a Mode (normally expressed by enums) from its implementation in commands
Share
It is possible to decouple context (switching descission, parameters) from the strategy
to solve the request (input) and its response (output).
You could use e.g. generics to solve this problem for different cases with one code base.
For example the strategy is defined by its kind of input and output and a condition to evaluate:
The context has different strategies and asks the strategies for their responsibility
(given conditions are ok, can calculate the result for an request).
Let’s take a simple example where an input string needs to be resolved to an output string
(like a switch case).
1. Define your actions (for simplification this is an (as you can see, every Method has its own logic and concern):
2. Define a condition to run the evaluation (lika a CanExecute of an ICommand):
… you could even define more conditions (e.g. one condition eval function per strategy)
but we use only one here.
3. Create strategy objects required by the context (these symbolize the different paths of the switch and they give us the result):
2. Initialize your context (represents the switch body with the input):
3. Wire them into the code (e.g. execute “switch” by a button click):
… that’s it.
The context gives you the right strategy (can do the right actions or give the “tools” you need to do some actions (e.g. an ICommand).