简体   繁体   English

策略设计模式与简单接口抽象?

[英]The Strategy design pattern vs simple Interface abstraction?

AFAIK , the strategy design pattern is pretty simple : AFAIK,策略设计模式非常简单

Interface : 介面

public interface IStrategy
    {
        object DoAlgorithm(object data);
    }

Implementing classes : 实施类:

lass ConcreteStrategyA : IStrategy
    {
        public object DoAlgorithm(object data)
        {
            var list = data as List<string>;
            list.Sort();
            return list;
        }
    }


 class ConcreteStrategyB : IStrategy
    {
        public object DoAlgorithm(object data)
        {
            var list = data as List<string>;
            list.Sort();
            list.Reverse();

            return list;
        }
    }

A context class which gets IStrategy in ctor : 一个在ctor中获取IStrategy的上下文类:

class Context
    {

        private IStrategy _strategy;


        public Context(IStrategy strategy)
        {
            this._strategy = strategy;
        }

        public void SetStrategy(IStrategy strategy)
        {
            this._strategy = strategy;
        }

        public void DoSomeBusinessLogic()
        {
            ////
        }
    }

And of course , the Main method: 当然,Main方法:

var context = new Context();
Console.WriteLine("Client: Strategy is set to normal sorting.");
context.SetStrategy(new ConcreteStrategyA());
context.DoSomeBusinessLogic();

Question: 题:

OK , But how does it make any difference as opposed to : 好的,但是相对于:

Istrategy context = new ConcreteStrategyA (); //or ConcreteStrategyB
Console.WriteLine("Client: Strategy is set to normal sorting.");
context.DoSomeBusinessLogic();

Am I missing something ? 我想念什么吗? why not just using interfaces? 为什么不仅仅使用接口?

You may need to do other things aside from the strategy... For example if you needed to add logging, or print statistics about your list - you probably wouldn't want to roll that into your strategy. 除策略外,您可能还需要做其他事情...例如,如果您需要添加日志记录或打印有关列表的统计信息-您可能不想将其纳入策略。 You may also want to dynamically add or let the user choose strategies, for example by loading 'plugins'. 您可能还希望动态添加或让用户选择策略,例如通过加载“插件”。

Generally you would use strategy for more short lived and volatile dependencies which may need to change, where as an interface would be used for larger or more static piece of functionality. 通常,您将使用策略来处理可能需要更改的更短寿命和易变的依赖项,因为接口将用于更大或更静态的功能。 The difference of for example runtime vs configuration flexibility. 例如运行时与配置灵活性之间的差异。

To use your example of a List Sorter, the strategy may take an IEnumerable and sort it, but you may want to define additional functionality in the above ListSorter class that finally uses that strategy, for example taking different input collections and presenting them to the sorting strategy, or normalizing them somehow - in this way you decouple your code, as the component requesting the list be sorted doesn't care how the component ends up sorting it, and the sorting code doesn't need to know about the normalization etc. Of course this is more useful in larger applications and in combination with other techniques, so is hard to demonstrate in the trivial example like this, but hopefully gives you an idea. 若要使用列表排序器的示例,该策略可以采用IEnumerable并将其排序,但是您可能希望在上述ListSorter类中定义其他功能,最终使用该策略,例如,采用不同的输入集合并将其呈现给排序策略或以某种方式对其进行规范化-通过这种方式,您可以使代码解耦,因为请求列表排序的组件并不关心组件最终如何对其进行排序,并且排序代码无需了解规范化等信息。当然,这在较大的应用程序中以及与其他技术结合使用时更有用,因此很难在这样的琐碎示例中进行演示,但希望能给您一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM