“策略设计模式"仅仅是多态性的基本使用吗? [英] Is 'Strategy Design Pattern' no more than the basic use of polymorphism?

查看:42
本文介绍了“策略设计模式"仅仅是多态性的基本使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

策略设计模式中,我们所做的是

  • 创建一个通用接口.
  • 使用该接口和重写的方法实现一组类.
  • 让运行时为与该公共接口具有相同类型的对象选择实际类,并调用将根据类正确解析的覆盖方法.

我的问题是,这不是我们学习的多态性和方法覆盖的基本示例吗?

除了可以使用抽象类,替换通用接口之外.

other than the possibility of using an abstract class too, replacing the common interface.

推荐答案

你描述的是一种实现策略模式的方法.您还描述了如何实现很多不同的设计,因为我们可能想要创建一个通用接口、进行不同的实现并在运行时为不同的情况选择一个的原因有很多很多.

What you describe is a way to implement the strategy pattern. You also describe how to implement a whole lot of different designs, since there are many, many reasons why we might want to create a common interface, make different implementations, and select one at runtime for different situations.

还有其他实现策略模式的方法.

There are also other ways to implement the strategy pattern.

但是,您知道,设计不是代码.设计是软件如何工作的心智模型——一个人的东西,而不是比特.设计模式是为常见问题组合解决方案的常用方法.它再次发生在您的脑海中,而不是位.

But, you know, a design is not code. A design is a mental model of how the software works -- a human thing, not bits. A design pattern is a common way of composing solutions to common sorts of problems. Again it happens in your head and not in bits.

特别是策略模式是关于使用可互换算法制作对象,其中任何一种都可以用于特定目的.

The strategy pattern in particular is about making objects with interchangeable algorithms, any of which could be used for a particular purpose.

所以,是的,您定义了一个接口……但它不是数据对象的接口——它没有 getter 和 setter 以及状态变更器等.接口定义了对象如何与算法交互用于特定目的.国际象棋游戏可能使用玩家"策略,例如,使用名为SelectNextMove"的方法.

So, yes, you define an interface... but it's not the interface to a data object -- it doesn't have getters and setters and state mutators, etc. The interface defines how the object interacts with the algorithm that is used for a particular purpose. A chess game might use a "Player" strategy, for example, with a method called "SelectNextMove".

而且,是的,您实现了该接口,但实现不是成为包含对象部分"的事物",它们是不同的算法,可用于执行对象所需的任何功能.国际象棋游戏可以支持许多不同的策略,这些策略可用于为计算机玩家选择下一步行动.

And, yes you make implementations of that interface, but the implementations aren't "things" that become "parts" of the containing object, they are different algorithms that could be used to perform whatever function the object requires. A chess game could support many different strategies that could be used to select the next move for the computer player.

因此,例如,当您在脑海中考虑国际象棋游戏的设计时,您可能会发现将选择下一步棋的策略与对棋盘建模的对象分开考虑并确保选择的动作被正确记录、传达和渲染.选择下一步行动的策略与这些无关.

So when you're thinking about the design of a chess game, for example, in your head, you will probably find it useful to think about strategy for choosing the next move separately from the objects that model the board and ensure that the selected move is recorded, communicated, and rendered properly. The strategy for choosing the next move is independent of these things.

如果你是一个优秀的软件设计师,你也会独立编写代码,这样代码中的关注点分离就会反映你头脑中的关注点分离,让维护代码的人更容易思考最有用的方式,并允许在您需要时换入和换出选择下一步行动的新策略.

If you're a good software designer, you will code it independently too, so that the separation of concerns in code will mirror the separation of concerns in your head, make it easy for people maintaining the code to think about it in the most useful way, and allow new strategies for choosing the next move to be swapped in and out whenever you want.

这篇关于“策略设计模式"仅仅是多态性的基本使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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