WEB开发网
开发学院软件开发C语言 适合C# Actor的消息执行方式(3):中看不中用的解... 阅读

适合C# Actor的消息执行方式(3):中看不中用的解决方案

 2010-09-30 20:50:26 来源:WEB开发网   
核心提示: F#的实现本文描述的方式也可以运用在在F#中,首先自然还是接口的定义:typeIPingMessageHandler=abstractPing:Ping->unitabstractFinish:unit->unitandIPongMessageHandler=abstractPong

F#的实现

本文描述的方式也可以运用在在F#中。首先自然还是接口的定义:

type IPingMessageHandler = 
  abstract Ping : Ping -> unit 
  abstract Finish : unit -> unit 
 
and IPongMessageHandler = 
  abstract Pong : Pong -> unit

以上便是F#中定义接口的方式,与C#相比更为简洁。接着便是Ping类型的实现:

and Ping() = 
  inherit (IPongMessageHandler -> unit) Actor() 
  let mutable count = 5 
  override self.Receive(message) = message self 
 
  member self.Start(pong : Pong) = 
    pong << fun h -> self |> h.Ping 
   
  interface IPongMessageHandler with 
    member self.Pong(pong) = 
      printfn "Ping received pong" 
      count <- count - 1 
      if (count > 0) then 
        pong << fun h -> self |> h.Ping 
      else 
        pong << fun h -> h.Finish() 
        self.Exit()

Pong类型的实现则更为简单:

and Pong() = 
  inherit (IPingMessageHandler -> unit) Actor() 
  override self.Receive(message) = message self 
   
  interface IPingMessageHandler with 
    member self.Ping(ping) = 
      printfn "Pong received ping" 
      ping << fun h -> self |> h.Pong 
     
    member self.Finish() = 
      printfn "Finished" 
      self.Exit()

启动乒乓测试:

(new Pong()) |> (new Ping()).Start;

出处:http://www.cnblogs.com/JeffreyZhao/archive/2009/07/17/message-execution-model-for-c-sharp-actor-3-nice-solution-with-little-use.html

上一页  1 2 3 4 5 6 

Tags:适合 Actor 消息

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接