调度功能
调度函数到底是什么?我用谷歌搜索过它们,一切都很模糊。它们似乎只是其他函数内的嵌套块/闭包?从 scala/lift 点来说……但我认为它是通用的,我也看到它们在 ruby 中提到过。
What exactly are dispatch functions? I've googled them and all is vague. They seem to just be nested blocks/closures inside of other functions? Speaking from a scala/lift point..but i assume it's universal, i've seen them mentioned in ruby as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调度的目标是动态地决定在函数中做什么。
当您有(动态)调度函数时,它的主要(或仅当您不需要转换或其他转换时)职责是决定调用哪个其他函数。该决定通常基于调用该方法的实例的类型或某些参数的类型,但它也可以取决于参数的值或某些配置值。
调度规则可以是硬编码的(例如使用scala中的模式匹配),或者可以来自调度表。
正如您提到的,有几种变体,例如单分派(具体方法取决于调用原始方法的实例,这是基本的 OO 机制),双分派(根据运行时类型将函数调用分派给不同的具体函数)调用涉及多个对象)。
一个相关的设计模式是访问者,它允许您向现有类动态添加一组函数,并且其核心还具有动态调度。
当您在调度方法内部或在某些初始化代码(例如调度表)中定义具体方法时,会出现嵌套块/闭包。
调度基于参数值、具有硬编码决策和调度表的情况的简单示例:
The goal of dispatching is to dynamically decide what to do in your function.
When you have a (dynamic) dispatch function it's main (or only, if you don't need casting or other conversions) responsibility is to decide which other function to call. The decision is often based on the type of the instance the method is called on, or the type of some of the parameters, but it can also depend e.g. on the value of the parameter(s), or some config values.
The dispatching rule can be hardcoded (using e.g. pattern matching in scala), or may come from a dispatch table.
As you mentioned there are several variations, like single dispatch (concrete method depends on the instance the original method is called on, which is a basic OO mechanism), double dispatch (dispatches a function call to different concrete functions depending on the runtime types of multiple objects involved in the call).
A related design pattern is the Visitor, which allows you to add a set of functions dynamically to existing classes and which also has dynamic dispatch at its core.
The nested blocks/closures appear when you define the concrete method inside of the dispatch method, or in some initialization code (e.f. for the dispatch table).
A simple example for the case when dispatching is based on the value of the parameter, with hardcoded decision and with dispatch table:
调度是 Lift 用于调度 Web 服务请求的术语。
使用 RestHelper 定义调度函数的最简单方法(请参阅 http://www.assembla。 com/wiki/show/liftweb/REST_Web_Services )
例如:
然后在 Boot.scala 中:
希望这会有所帮助。
Dispatch is the term Lift uses for dispatching web services requests.
The easiest way to define the dispatch function using RestHelper (see http://www.assembla.com/wiki/show/liftweb/REST_Web_Services )
For example:
Then in Boot.scala:
Hope this helps.