Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6192337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:55:34+00:00 2026-05-24T02:55:34+00:00

How to decouple a Mode (normally expressed by enums) from its implementation in commands

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:55:34+00:00Added an answer on May 24, 2026 at 2:55 am

    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:

    public class Strategy<TInput, TOutput>
    {
        public Predicate<TInput> Condition { get; private set; }
        public Func<TInput, TOutput> Result { get; private set; }
    
        public Strategy(Predicate<TInput> condition, Func<TInput, TOutput> result)
        {
            Condition = condition;
            Result = result;
        }
    
        public TOutput Evaluate(TInput input)
        {
            return Condition(input) ? Result(input) : default(TOutput);
        }
    }
    

    The context has different strategies and asks the strategies for their responsibility
    (given conditions are ok, can calculate the result for an request).

    public class Context<TInput, TOutput>
    {
        private List<Strategy<TInput, TOutput>> Strategies { get; set; }
    
        public Context(params Strategy<TInput, TOutput>[] strategies)
        {
            Strategies = strategies.ToList();
        }
    
        public TOutput Evaluate(TInput input)
        {
            var result = default(TOutput);
            foreach (var strategy in Strategies)
            {
                result = strategy.Evaluate(input);
    
                if (!Equals(result, default(TOutput)))
                    break;
            }
    
            return result;
        }
    }
    

    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):

        private static string Move(string s)
        {
            if (s == "move")
                return "doMove"; // here could be some more action...
            return null;
        }
    
        private static string Query(string s)
        {
            if (s == "point")
                return "queryPoint"; // here could be some more action...
            return null;
        }
    

    2. Define a condition to run the evaluation (lika a CanExecute of an ICommand):

        private static bool Condition(string s)
        {
            return !string.IsNullOrEmpty(s); // could eval different states, values
        }
    

    … 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):

     var navigate = new Strategy<string, string>(Condition, Move);
     var query = new Strategy<string, string>(Condition, Query);
    

    2. Initialize your context (represents the switch body with the input):

     var strategies = new List<Strategy<string, string>> {navigate, query};
     var context = new Context<string, string>(strategies.ToArray());
    

    3. Wire them into the code (e.g. execute “switch” by a button click):

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            var message = context.Evaluate(textBox1.Text);
    
            if (message != null) MessageBox.Show(message); // display result
        }
    

    … 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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was taught to decouple code implementation from database implementation through the use of
Is it a good practise to decouple input checking from a model and have
I understand the principles of Coding to Interfaces - to decouple the implementation from
I have just learned the Bridge Pattern and its intent : Decouple an abstraction
I'm looking for a good strategy to truly decouple, for parallel processing, my web
From PyPubSub : Pypubsub provides a simple way for your Python application to decouple
I am refactoring some code to decouple GUI from some state. #include <StateObject> Class
in this MVC tutorial it showed how to decouple Controller from database logic using
This is a follow-on from a previous question I had: How to decouple my
As far as I understand, the purpose of a Bridge pattern is, quoting from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.