当前位置:编程学习 > asp >>

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

 

策略模式:

 

        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:

        

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,