适合C# Actor的消息执行方式(3):中看不中用的解决方案
2010-09-30 20:50:26 来源:WEB开发网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
- ››适合做商品团购营销的网站
- ››适合所有浏览器hack的CSS技巧
- ››消息称中国移动即将获得iPhone 4销售权
- ››适合C# Actor的消息执行方式(1):Erlang中的模式...
- ››适合C# Actor的消息执行方式(2):C# Actor的尴尬...
- ››适合C# Actor的消息执行方式(3):中看不中用的解...
- ››适合C# Actor的消息执行方式(4):阶段性总结
- ››适合C# Actor的消息执行方式(5):一个简单的网络...
- ››适合C# Actor的消息执行方式(6):协变与逆变
- ››适合1-5个月经验的seo优化全过程分享
- ››消息称联通高层赴美谈引入iPhone4 将带WiFi
- ››消息称微软将在近期发布IE9 beta
更多精彩
赞助商链接