如何去耦模式切换和命令 [英] How to decouple mode switching and commands

查看:155
本文介绍了如何去耦模式切换和命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从它的命令执行情况和他们的关系解耦模式(通过枚举pssed正常前$ P $)?
他们是一个很好的模式描述模式切换之间松散的结合(整型,枚举,字符串,...)和它的命令拨打电话?我想通过配置添加模式,所以这一定是(动态)易扩展(无需编程)。我已经知道命令模式(ICommand的在C#/。NET)。这可能是命令的解释及其相关模式数字,但对于切换逻辑?

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?

推荐答案

有可能从战略脱钩上下文(开关descission,参数)
解决请求(输入)和它的响应(输出)。

It is possible to decouple context (switching descission, parameters) from the strategy to solve the request (input) and its response (output).

您可以使用例如仿制药来解决这个问题不同的情况与一个code基地。
例如,策略由同类的输入和输出,并评估条件的定义:

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);
    }
}

上下文有不同的策略,并要求战略为己任
(给定的条件都ok,可以计算的请求的结果)。

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。定义你的操作(为了简化这是一个(你可以看到,每一个方法都有其自身的逻辑和关注):

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。定义条件运行评估(利卡一个ICommand的CanExecute):

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
    }

...你甚至可以定义更多的条件(每战略例如一个条件eval函数)
但我们只使用一个在这里。

... you could even define more conditions (e.g. one condition eval function per strategy) but we use only one here.

3。创建策略对象按上下文(这些象征着开关的不同路径,他们给我们的结果)要求:

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。初始化上下文(重presents开关本体与输入):

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。线材他们到code (例如,通过点击按钮执行开关):

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
    }

...就是这样。

上下文为您提供了正确的策略(可以做正确的行动或带来的工具,你需要做一些动作(例如一个ICommand)。

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

这篇关于如何去耦模式切换和命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆