调度功能

发布于 2024-09-02 14:23:49 字数 101 浏览 7 评论 0原文

调度函数到底是什么?我用谷歌搜索过它们,一切都很模糊。它们似乎只是其他函数内的嵌套块/闭包?从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不可一世的女人 2024-09-09 14:23:49

调度的目标是动态地决定在函数中做什么。

当您有(动态)调度函数时,它的主要(或仅当您不需要转换或其他转换时)职责是决定调用哪个其他函数。该决定通常基于调用该方法的实例的类型或某些参数的类型,但它也可以取决于参数的值或某些配置值。

调度规则可以是硬编码的(例如使用scala中的模式匹配),或者可以来自调度表。

正如您提到的,有几种变体,例如单分派(具体方法取决于调用原始方法的实例,这是基本的 OO 机制),双分派(根据运行时类型将函数调用分派给不同的具体函数)调用涉及多个对象)。

一个相关的设计模式是访问者,它允许您向现有类动态添加一组函数,并且其核心还具有动态调度。

当您在调度方法内部或在某些初始化代码(例如调度表)中定义具体方法时,会出现嵌套块/闭包。

调度基于参数值、具有硬编码决策和调度表的情况的简单示例:

   class Dispatch {

     def helloJohn(): String = "Hello John"

     def helloJoe(): String = "Hello Joe"

     def helloOthers(): String = "Hello"

     def sayHello(msg: String): String = msg match {
       case "John" => helloJohn()
       case "Joe" => helloJoe()
       case _ => helloOthers()
     }


     val fs = Map("John" -> helloJohn _, "Joe" -> helloJoe _)

     def sayHelloDispatchTable(msg: String): String = fs.get(msg) match {
         case Some(f) => f()
         case _ => helloOthers()
     }
   }

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:

   class Dispatch {

     def helloJohn(): String = "Hello John"

     def helloJoe(): String = "Hello Joe"

     def helloOthers(): String = "Hello"

     def sayHello(msg: String): String = msg match {
       case "John" => helloJohn()
       case "Joe" => helloJoe()
       case _ => helloOthers()
     }


     val fs = Map("John" -> helloJohn _, "Joe" -> helloJoe _)

     def sayHelloDispatchTable(msg: String): String = fs.get(msg) match {
         case Some(f) => f()
         case _ => helloOthers()
     }
   }
零時差 2024-09-09 14:23:49

调度是 Lift 用于调度 Web 服务请求的术语。

使用 RestHelper 定义调度函数的最简单方法(请参阅 http://www.assembla。 com/wiki/show/liftweb/REST_Web_Services

例如:

object MyRestService extends RestHelper {
  serve {
    case "api" :: "user" :: AsLong(id) :: _ XmlGet _ => <b>ID: {id}</b>
    case "api" :: "user" :: AsLong(id) :: _ JsonGet _ => JInt(id)
  }
}

然后在 Boot.scala 中:

LiftRules.dispatch.append(MyRestService)

希望这会有所帮助。

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:

object MyRestService extends RestHelper {
  serve {
    case "api" :: "user" :: AsLong(id) :: _ XmlGet _ => <b>ID: {id}</b>
    case "api" :: "user" :: AsLong(id) :: _ JsonGet _ => JInt(id)
  }
}

Then in Boot.scala:

LiftRules.dispatch.append(MyRestService)

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文