`
jgsj
  • 浏览: 962476 次
文章分类
社区版块
存档分类
最新评论

策略模式

 
阅读更多

  一、定义

  策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。

  以上的定义会比较抽象。下面我来说一个具体的例子。假设有很多个人早上去上学,每个人都会有如何去学校的策略(如走路、公交、骑车),同学到了学校以后,老师把他们来学校的方法记录下来。假设有A、B、C三类学生,分别是走路、公交、骑车来学校,由于每一个学生类一定有来学校的策略,因此他们可以继承于同一个接口,接口中定义一个来学校的方法,接下来,只要老师能够调用这个方法,就能够知道他们各自来学校的方法了。

  C#代码如下:

using System;
namespace Demo
{
    interface IHowToGoToSchool
    {
        void HowToGo();
    }

    class StudentA : IHowToGoToSchool
    {
        public void HowToGo()
        {
            Console.WriteLine("By bus.");
        }
    }

    class StudentB : IHowToGoToSchool
    {
        public void HowToGo()
        {
            Console.WriteLine("By bike.");
        }
    }

    class StudentC : IHowToGoToSchool
    {
        public void HowToGo()
        {
            Console.WriteLine("Walking.");
        }
    }

    class Teacher
    {
        public void Record(IHowToGoToSchool method)
        {
            method.HowToGo();
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            Teacher teacher = new Teacher();
            StudentA a = new StudentA();
            StudentB b = new StudentB();
            StudentC c = new StudentC();
            teacher.Record(a);
            teacher.Record(b);
            teacher.Record(c);
        }
    }
}

  Java的代码如下:

interface IHowToGoToSchool
{
    void HowToGo();
}

class StudentA implements IHowToGoToSchool
{
    public void HowToGo()
    {
        System.out.println("By bus.");
    }
}

class StudentB implements IHowToGoToSchool
{
    public void HowToGo()
    {
    	System.out.println("By bike.");
    }
}

class StudentC implements IHowToGoToSchool
{
    public void HowToGo()
    {
    	System.out.println("Walking.");
    }
}

class Teacher
{
    public void Record(IHowToGoToSchool method)
    {
        method.HowToGo();
    }
}

public class Program
{
    public static void main(String[] args)
    {
        Teacher teacher = new Teacher();
        StudentA a = new StudentA();
        StudentB b = new StudentB();
        StudentC c = new StudentC();
        teacher.Record(a);
        teacher.Record(b);
        teacher.Record(c);
    }
}

  我们可以看到,使用策略模式的好处在于降低了程序的耦合度。Teacher类中的Record会传入一个继承了IHowToGoToSchool的对象,因此这个对象一定会有HowToGo这个方法。我们不必在Teacher类中关心这个方法的实现——因为它有StudentA、StudentB、StudentC各自的实现方法。如果我们需要增加一个新的学生类StudentD,只需要继承IHowToGoToSchool,然后实现HowToGo方法即可。

  二、另一个例子——方法回调(函数回调)

  设计模式中许多模式是类似的。下面举一个用策略模式来实现一个方法回调。(回调机制到底是单独算一个设计模式,还是算策略模式,还是算观察者模式,在我看来存在争议,主要是在于其用在什么场景下而定。就这个例子而言,我觉得它代表了策略模式的思想)

  C#代码如下:

using System;
namespace Demo
{
    public interface ICallBack
    {
        void Invoke();
    }

    public class Downloader
    {
        public static void Download(String url, ICallBack callback)
        {
            Console.WriteLine("正在下载:{0}", url);
            callback.Invoke();
        }
    }

    public class DownloadedShutdown : ICallBack
    {
        public void Invoke()
        {
            Console.WriteLine("下载完成,现在即将关机");
        }
    }

    public class DownloadedGetNext : ICallBack
    {
        public void Invoke()
        {
            Console.WriteLine("下载完成,现在下载下一个文件");
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            ICallBack shutDown = new DownloadedShutdown();
            ICallBack getNext = new DownloadedGetNext();
            Downloader.Download("www.qq.com/s.html", shutDown);
            Downloader.Download("www.qq.com/s.html", getNext);
        }
    }
}

  Java代码如下:

interface ICallBack
{
    void Invoke();
}

class Downloader
{
    public static void Download(String url, ICallBack callback)
    {
        System.out.println("正在下载:" + url);
        callback.Invoke();
    }
}

class DownloadedShutdown implements ICallBack
{
    public void Invoke()
    {
    	System.out.println("下载完成,现在即将关机");
    }
}

class DownloadedGetNext implements ICallBack
{
    public void Invoke()
    {
    	System.out.println("下载完成,现在下载下一个文件");
    }
}

public class Program
{
    public static void main(String[] args)
    {
        ICallBack shutDown = new DownloadedShutdown();
        ICallBack getNext = new DownloadedGetNext();
        Downloader.Download("www.qq.com/s.html", shutDown);
        Downloader.Download("www.qq.com/s.html", getNext);
    }
}


  我们在执行Downloader.Download时,传入了ICallback,在Download方法执行到最后,会调用这个ICallback对象的Invoke方法,因此我们可以看成,代码中的DownloadedShutdown类和DownloadedGetNext类都是回调的策略,它们分别表示了下载完后关机、下载完后继续下载两种策略。

  三、C#中策略模式的替代方法

  在C#中,你可能会想,这种回调完全可以用委托、事件,甚至Lambda表达式来实现,事实上确实这样能够简化很多代码。在C#中有代表意义的就是多线程类(Thread)。下面是一个C#多线程最简单的例子:

using System;
using System.Threading;
namespace Demo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Thread t = new Thread(ThreadMethod);
            t.Start();
        }

        public static void ThreadMethod()
        {
            Console.WriteLine("调用了一个多线程方法。");
        }
    }
}

  C#中存在方法的委托,因此可以把方法名当做参数传入,十分方便,而且我们不用为了实现某个接口而重新写一个类。

  另外一个例子是C#中的泛型容器List<T>.Sort方法。它是用于排序的一个方法,由于泛型容器的类型不确定,为了满足对特定类型对象进行排序,可以在Sort方法中传入比较器。比较器主要有两种,第一种比较器是一个继承了IComparer<T>接口的对象,这个就如之前所提到,我们要构造一个继承了这个接口的类,然后实现Compare方法,在调用List<T>.Sort的时候,它就会通过调用IComparer<T>.Compare来实现排序;第二种比较器是Comparison<in T>,它是一个委托,也就是说,你不用辛辛苦苦重新写一个新的类,而是只要写一个满足Comparison<in T>委托的方法就可以了。

  一下有个例子。每个学生都拥有一个Id,现在将学生簿List<Student>中的所有元素按照学生Id进行排序。下面将用C# 分别为Sort方法传入IComparer<T>和Comparison<in T>来实现排序:

using System.Collections.Generic;
using System;
namespace Demo
{
    public class Student
    {
        public int Id { get; set; }
        public Student(int id)
        {
            Id = id;
        }
    }

    public class StudentSortStrategy : IComparer<Student>
    {
        public int Compare(Student x, Student y){
            return x.Id - y.Id;
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            List<Student> students = new List<Student>();
            students.Add (new Student(5));
            students.Add (new Student(2));
            students.Add(new Student(3));
            Console.WriteLine ("Students after sorted:");
            students.Sort(new StudentSortStrategy());
            foreach (Student s in students)
            {
                Console.WriteLine(s.Id );
            }

            List<Student> students2 = new List<Student>();
            students2.Add(new Student(5));
            students2.Add(new Student(2));
            students2.Add(new Student(3));
            Console.WriteLine("Students2 after sorted:");
            students2.Sort(Compare);
            foreach (Student s in students2)
            {
                Console.WriteLine(s.Id);
            }
        }

        private static int Compare(Student x, Student y)
        {
            return x.Id - y.Id;
        }
    }
}


  输出结果为:

Students after sorted:

2

3

5

Students2 after sorted:

2

3

5

  可见,在C#中使用委托(方法名)可以实现策略模式。

  四、关于Java的策略模式

  Java中没有委托这么一说,因此策略模式就显得十分有用了。例如:在Java中实现多线程有两种方法,第一种是继承Thread类,第二种是继承Runnable接口。Runnable接口中定义了方法run,我们需要做的,是实现这个run,在调用Thread对象的start方法时,其实它是用多线程的方法调用了传入的Runnable接口对象的run方法。下面给出继承于Runnable接口的Java代码:

public class Program implements Runnable{
	public static void main(String[] args){
		Thread t = new Thread(new Program());
		t.start();
	}

	@Override
	public void run() {
		System.out.println("调用了一个多线程方法。");
	}
}

  在C#中,我们直接为Thread的构造函数中传入一个委托即可,而事实上,C#中的Thread类为密封类(也就是Java中的final class),我们无法进行继承,也无需进行继承。

  五、总结

  策略模式是一个很常用的模式,尤其是对于Java这种没有直接的委托机制的语言来说。设计模式中有许多模式的实现是相近的,因此在合适的场景下灵活地使用好它们是最重要的。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics