尝试使用 new Func来理解维基百科策略模式示例

发布于 2024-08-18 00:38:55 字数 1547 浏览 1 评论 0原文

我正在看这个, http://en.wikipedia.org/wiki/Strategy_pattern 和我理解策略模式的概念,但有人可以解释一下 C# 示例吗?

我真的不明白 Context 类中“策略”定义的方式和原因,为什么它是 Func 但随后只传递了两个参数,例如 8,9 ?

static void Main(string[] args)
{

    var context = new Context<int>();

    // Delegate
    var concreteStrategy1 = new Func<int, int, int>(PerformLogicalBitwiseOr);

    // Anonymous Delegate
    var concreteStrategy2 = new Func<int, int, int>(
        delegate(int op1, int op2)
        {
            return op1 & op2;
        });

    // Lambda Expressions
    var concreteStrategy3 = new Func<int, int, int>((op1, op2) => op1 >> op2);
    var concreteStrategy4 = new Func<int, int, int>((op1, op2) => op1 << op2);

    context.Strategy = concreteStrategy1;
    var result1 = context.Execute(8, 9);

    context.Strategy = concreteStrategy2;
    var result2 = context.Execute(8, 9);

    context.Strategy = concreteStrategy3;
    var result3 = context.Execute(8, 1);

    context.Strategy = concreteStrategy4;
    var result4 = context.Execute(8, 1);

}

static int PerformLogicalBitwiseOr(int op1, int op2)
{
    return op1 | op2;
}

class Context<T>
{
    public Func<T, T, T> Strategy { get; set; }

    public T Execute(T operand1, T operand2)
    {
        return this.Strategy != null
            ? this.Strategy(operand1, operand2)
            : default(T);
    }
}

I was looking at this, http://en.wikipedia.org/wiki/Strategy_pattern and I understand the concept of the strategy pattern, but could someone explain the C# example a bit.

I dont really get the how and why of the definition of 'Strategy' in the Context class, why is it Func<T, T, T> but then just two params are passed in eg 8,9 ?

static void Main(string[] args)
{

    var context = new Context<int>();

    // Delegate
    var concreteStrategy1 = new Func<int, int, int>(PerformLogicalBitwiseOr);

    // Anonymous Delegate
    var concreteStrategy2 = new Func<int, int, int>(
        delegate(int op1, int op2)
        {
            return op1 & op2;
        });

    // Lambda Expressions
    var concreteStrategy3 = new Func<int, int, int>((op1, op2) => op1 >> op2);
    var concreteStrategy4 = new Func<int, int, int>((op1, op2) => op1 << op2);

    context.Strategy = concreteStrategy1;
    var result1 = context.Execute(8, 9);

    context.Strategy = concreteStrategy2;
    var result2 = context.Execute(8, 9);

    context.Strategy = concreteStrategy3;
    var result3 = context.Execute(8, 1);

    context.Strategy = concreteStrategy4;
    var result4 = context.Execute(8, 1);

}

static int PerformLogicalBitwiseOr(int op1, int op2)
{
    return op1 | op2;
}

class Context<T>
{
    public Func<T, T, T> Strategy { get; set; }

    public T Execute(T operand1, T operand2)
    {
        return this.Strategy != null
            ? this.Strategy(operand1, operand2)
            : default(T);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

℉絮湮 2024-08-25 00:38:55

Func 是以下形式的委托:

TResult function(T1 arg1, T2 arg2)

因此,Func 有 2 个参数类型和 1 个返回类型。因此,当使用该函数时,您将输入

 (arg1, arg2) => return new TResult();

A Func<T1,T2,TResult> is a delegate in the form of:

TResult function(T1 arg1, T2 arg2)

so, the Func has 2 argument types, and 1 return type. Therefore when consuming the func, you'll type

 (arg1, arg2) => return new TResult();
半寸时光 2024-08-25 00:38:55

Func 是一个接受两个 int 参数并返回一个 int 的 func - Func 定义中的最后一个类型是返回类型。

Func<int, int, int> is a func that take two int arguments and returns an int - the last type in a Func definition is the return type.

随波逐流 2024-08-25 00:38:55

Func 是委托。委托是一种代表单个函数的类型。在 C# 中,您可以使用这样的函数,而不是声明特定的接口。如果您愿意,您可以使用如下所示的界面:

interface IStrategy
{
     T Compute<T1, T2>();
}

A Func<T, T1, T2> is a delegate. A delegate is a type which represents a single function. In C# you can use functions like this instead of declaring a specific interface. If you wanted, you could use an interface which would look something like this:

interface IStrategy
{
     T Compute<T1, T2>();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文