.NET类库中发现设计模式:策略模式

来源:岁月联盟 编辑:exp 时间:2011-11-10

 

策略模式:

 

        The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.(策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。)

 

/

 

Context(应用场景):

 

 

    需要使用ConcreteStrategy提供的算法。

 

    内部维护一个Strategy的实例。

 

    负责动态设置运行时Strategy具体的实现算法。

 

    负责跟Strategy之间的交互和数据传递。

 

Strategy(抽象策略类):

 

    定义了一个公共接口,各种不同的算法以不同的方式实现这个接口,Context使用这个接口调用不同的算法,一般使用接口或抽象类实现。

 

ConcreteStrategy(具体策略类):

 

    实现了Strategy定义的接口,提供具体的算法实现。

 

 

 

废话少说,到.NET类库中找找策略模式

 

    ArrayList类肯定都会用过吧,那么它的Sort方法想必大家也一定不陌生了。Sort方法的定义如下:

 

    public virtual void Sort (IComparer comparer)

 

    可以看到Sort方法接收一个IComparer类型的参数,那么这个IComparer接口是做什么用的呢?下面我们看一段程序,下面的代码示例演示如何使用默认比较器和一个反转排序顺序的自定义比较器,对ArrayList 中的值进行排序:

 

   using System;

   using System.Collections;

 

    public class SamplesArrayList

    {

 

        public class myReverserClass : IComparer

        {

 

            // Calls CaseInsensitiveComparer.Compare with the parameters reversed.

            int IComparer.Compare(Object x, Object y)

            {

                return ((new CaseInsensitiveComparer()).Compare(y, x));

            }

 

        }

 

        public static void Main()

        {

 

            // Creates and initializes a new ArrayList.

            ArrayList myAL = new ArrayList();

            myAL.Add("The");

            myAL.Add("quick");

            myAL.Add("brown");

            myAL.Add("fox");

            myAL.Add("jumps");

            myAL.Add("over");

            myAL.Add("the");

            myAL.Add("lazy");

            myAL.Add("dog");

 

            // Displays the values of the ArrayList.

            Console.WriteLine("The ArrayList initially contains the following values:");

            PrintIndexAndValues(myAL);

 

            // Sorts the values of the ArrayList using the default comparer.

            myAL.Sort();

            Console.WriteLine("After sorting with the default comparer:");

            PrintIndexAndValues(myAL);

 

            // Sorts the values of the ArrayList using the reverse case-insensitive comparer.

            IComparer myComparer = new myReverserClass();

            myAL.Sort(myComparer);

            Console.WriteLine("After sorting with the reverse case-insensitive comparer:");

            PrintIndexAndValues(myAL);

 

        }

 

        public static void PrintIndexAndValues(IEnumerable myList)

        {

            int i = 0;

            foreach (Object obj in myList)

                Console.WriteLine("/t[{0}]:/t{1}", i++, obj);

            Console.WriteLine();

        }

 

    }

 

 

    /*

    This code produces the following output.

    The ArrayList initially contains the following values:

            [0]:    The

            [1]:    quick

            [2]:    brown

            [3]:    fox

            [4]:    jumps

            [5]:    over

            [6]:    the

            [7]:    lazy

            [8]:    dog

 

    After sorting with the default comparer:

            [0]:    brown

            [1]:    dog

            [2]:    fox

            [3]:    jumps

            [4]:    lazy

            [5]:    over

            [6]:    quick

            [7]:    the

            [8]:    The

 

    After sorting with the reverse case-insensitive comparer:

            [0]:    the

            [1]:    The

            [2]:    quick

            [3]:    over

            [4]:    lazy

            [5]:    jumps

            [6]:    fox

            [7]:    dog

            [8]:    brown

    */

 

    怎么样,大家看出来了吧,其实在这段代码里,ArrayList相当于Strategy模式中的Context(应用场景)部分,而IComparer相当于Strategy(抽象策略类)部分,myReverserClass相当于ConcreteStrategy(具体策略类)部分。我们这里抛开myReverserClass类的Compare方法如何具体实现不谈,我们只要知道这是一个具体策略类,它提供了应用场景需要的具体算法,它实现了抽象策略类接口,而应用场景通过抽象策略类动态调用到了具体策略类中的算法。哈!所以这是一个十分典型的Strategy模式的应用。

 

    基于这个符合Strategy模式的结构,我们还可以提供很多种自定义的具体策略类的实现,只要这些类实现了IComparer接口,就可以在运行时动态设置给ArrayList类的Sort方法,在Sort方法中会根据具体策略类实现的比较算法规则来对ArrayList中的数据进行排序。

 

 

 

策略模式应用场景和优缺点

    上面我们已经看过了Strategy模式的详细介绍,下面我们再来简单说说这个模式的优缺点吧!怎么说呢,人无完人,设计模式也不是万能的,每一个模式都有它的使命,也就是说只有在特定的场景下才能发挥其功效。我们要使用好模式,就必须熟知各个模式的应用场景。

 

    对于Strategy模式来说,主要有这些应用场景:

 

    1、  多个类只区别在表现行为不同,可以使用Strategy模式,在运行时动态选择具体要执行的行为。(例如FlyBehavior和QuackBehavior)

 

    2、  需要在不同情况下使用不同的策略(算法),或者策略还可能在未来用其它方式来实现。(例如FlyBehavior和QuackBehavior的具体实现可任意变化或扩充)

 

    3、  对客户(Duck)隐藏具体策略(算法)的实现细节,彼此完全独立。

 

    对于Strategy模式来说,主要有如下优点:

 

    1、  提供了一种替代继承的方法,而且既保持了继承的优点(代码重用)还比继承更灵活(算法独立,可以任意扩展)。

 

    2、  避免程序中使用多重条件转移语句,使系统更灵活,并易于扩展。

 

    3、  遵守大部分GRASP原则和常用设计原则,高内聚、低偶合。

 

    对于Strategy模式来说,主要有如下缺点:

 

    1、  因为每个具体策略类都会产生一个新类,所以会增加系统需要维护的类的数量。

 

最后一个设计原则

    关于Strategy模式的故事讲到这里,应该基本OK啦!下面我们再聊些更高层次的东西。什么是更高层次的东西?嘿!当然是设计原则了!在前面总结Strategy模式的优点的时候我们提到过,Strategy模式不仅保留了继承的优点,而且还提供了更灵活的扩展能力。为什么会这样呢?Strategy模式是怎么做到这一点的呢?哈!这是因为它“上面有人”啊!谁啊?它就是我们下面要介绍的重量级设计原则:

 

    Favor composition over inheritance.(优先使用对象组合,而非类继承)

 

    关于组合和继承,我们只要这样来理解即可:组合是一种“HAS-A”关系,而继承是一种“IS-A”关系。很明显“HAS-A”要比“IS-A”更灵活一些。也就是说在创建系统的时候,我们应该优先使用对象组合,因为它不仅可以给你提供更多灵活性和扩展性,而且还使你可以在运行时改变行为(组合不同的对象),这简直是酷毙了!但是也不是说继承就是不能用,只是说应该把继承应用在相对更稳定,几乎没有变化的地方,例如前面的Duck类里的Swim()方法,因为可以肯定所有鸭子一定都会游泳,所以就没有必要给这个行为提供基于Strategy模式的实现方式,因为那样做除了是程序更复杂以外,没有什么意义。