使用策略模式的好处在哪里? [英] Where is the benefit in using the Strategy Pattern?

查看:116
本文介绍了使用策略模式的好处在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过维基百科上的这个解释,特别是C ++示例,并且没有识别只定义3个类,创建实例并调用它们之间的区别,以及该示例。我看到的只是把另外两个课程放在这个过程中,看不到哪里会有好处。现在我确定我缺少一些明显的东西(树木的木材) - 有人可以用一个确定的现实世界的例子解释一下吗?

I've looked at this explanation on Wikipedia, specifically the C++ sample, and fail to recognize the difference between just defining 3 classes, creating instances and calling them, and that example. What I saw was just placing two other classes into the process and cannot see where there would be a benefit. Now I'm sure I'm missing something obvious (wood for the trees) - could someone please explain it using a definitive real-world example?

从目前的答案中我可以做些什么,在我看来,这只是一个更复杂的方式:

What I can make from the answers so far, it seems to me to be just a more complex way of doing this:

have an abstract class: MoveAlong with a virtual method: DoIt()
have class Car inherit from MoveAlong, 
     implementing DoIt() { ..start-car-and-drive..}
have class HorseCart inherit from MoveAlong, 
     implementing DoIt() { ..hit-horse..}
have class Bicycle inherit from MoveAlong, 
     implementing DoIt() { ..pedal..}
now I can call any function taking MoveAlong as parm 
passing any of the three classes and call DoIt
Isn't this what Strategy intents? (just simpler?)


上面提到的函数是替换为另一个类,其中MoveAlong将是根据需要设置的属性,该类是基于在此新类中实现的算法。 (类似于接受的答案所示)

The function I refer to above is replaced with another class in which MoveAlong would be attribute which is set according to need based on the algorithm implemented in this new class. (Similar to what is demonstrated in the accepted answer.)

结论

策略模式有其用途,但我对KISS很有信心,并且倾向于更直接和更少的混淆技术。主要是因为我想传递易于维护的代码(而且,我很可能是必须进行更改的人)。

The Strategy Pattern has it's uses, but I am a strong believer in KISS, and would tend to more straightforward and less obfuscatory techniques. Mostly since I want to pass on easily maintainable code (and 'cos I'll most likely be the one who have to make the changes!).

推荐答案

关键是将算法分离成可以在运行时插入的类。例如,假设你有一个包含时钟的应用程序。有许多不同的方法可以绘制时钟,但是大部分底层功能是一样的。所以你可以创建一个时钟显示界面:

The point is to separate algorithms into classes that can be plugged in at runtime. For instance, let's say you have an application that includes a clock. There are many different ways that you can draw a clock, but for the most part the underlying functionality is the same. So you can create a clock display interface:

class IClockDisplay
{
    public:
       virtual void Display( int hour, int minute, int second ) = 0;
};

然后你的Clock类被挂接到一个定时器,每秒钟更新一次时钟。所以你会有这样的东西:

Then you have your Clock class that is hooked up to a timer and updates the clock display once per second. So you would have something like:

class Clock
{
   protected:
      IClockDisplay* mDisplay;
      int mHour;
      int mMinute;
      int mSecond;

   public:
      Clock( IClockDisplay* display )
      {
          mDisplay = display;
      }

      void Start(); // initiate the timer

      void OnTimer()
      {
         mDisplay->Display( mHour, mMinute, mSecond );
      }

      void ChangeDisplay( IClockDisplay* display )
      {
          mDisplay = display;
      }
};

然后在运行时,使用正确的显示类来实例化你的时钟。即,您可以使用ClockDisplayDigital,ClockDisplayAnalog,ClockDisplayMartian都实现IClockDisplay界面。

Then at runtime you instantiate your clock with the proper display class. i.e. you could have ClockDisplayDigital, ClockDisplayAnalog, ClockDisplayMartian all implementing the IClockDisplay interface.

所以你可以通过创建一个新的类来添加任何类型的新时钟显示,而不必弄乱与您的Clock类,而不必覆盖可能会混乱维护和调试的方法。

So you can later add any type of new clock display by creating a new class without having to mess with your Clock class, and without having to override methods which can be messy to maintain and debug.

这篇关于使用策略模式的好处在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆