zoukankan      html  css  js  c++  java
  • Event System Dispatcher学习笔记

    备注:MessageDispatcher图关联Message图

    Dispatcher does this and makes it easy too! Simply tell the Dispatcher what an object wants to listen for. When another object sends that message to the Dispatcher, the Dispatcher will ensure all the "listeners" are notified. 

    简单来说,A对象监听Dispatcher,B对象发送信息给DispatcherDispatcher然后使A对象接收到B对象的信息。

    The DispatchManager class is used to collect messages and then send them out to "listeners" that you've registered with it. See the "Usage" section below. 

    DispatchManager类主要被用来收集信息,然后发送到那些被注册的监听者身上。

    Advanced 
    In addition to the simple approach shown above, the Dispatcher also allows you to create more complex listeners and messages. For example, objects can listen for messages that specify a specific filter or are addressed specifically to them. 

    Dispatcher允许创建复杂的监听者和信息,比如对象可以监听指定的已过滤的信息或者指定的地址。

    当一个messages被接收时,他包含了如下信息:

      Sender

      Receiver

      Message Type

      Customized Data

    Features include:

    • Create and send custom message types
    • Send messages containing custom data
    • Send messages to everyone
    • Send messages to objects based on thier name
    • Send messages to objects based on thier tag
    • Send messages to objects based on a string filter
    • Send a message immediately
    • Send a message at the next frame
    • Schedule a message for the future
    • Connect multiple listeners to a single message
    • High performance message pooling

    解析:

      基本流程:

        1.MessageDispatcher.AddListener()添加MessageListenerDefinition监听者的定义,在MessageDispatcher里将传递过来的MessageListenerDefinition处理添加到mListenerAdds的集合里,然后在MessageDispatcherStub(Mono)的Update方法里将所添加的mListenerAdds集合内的信息更新到mMessageHandlers中。

        2.MessageDispatcher.SendMessage()将传递过来的Message识别出来改变Message的状态,然后在该方法中触发对应的委托。

      备注:

        传递自己的自定义的类很也很简单,只要把类继承Message类就可以了,同理,继承IMessage接口也可以。

      MessageDispatcher.AddListener()中的参数rImmediate为true的话不会添加到MessageDispatcher的mListenerAdds集合中,而是直接更新到mMessageHandlers集合中。

      其中AddListener()参数的Object对象类型和其他的AddListener()方法基本一样,只不过用了对象的name和tag转换为rFilter去识别对象从而指定传递而已。

      RemoveListener()和AddListener()基本一样,会在mMessageHandlers移除或增加对应的处理,然后在MessageDispatcherStub(Mono)的Update方法里清空这两个列表,按帧清空(这样不会造成性能么)。

      MessageDispatcher.SendMessage()的object参数也和AddListener()一样,在识别Message将object参数转换回name和tag进行指定委托的处理。

    因此建议SendMessage()时直接定义自己的message(即使自己不创建,在方法内部也会自己创建,不过添加完后要 Message.Release释放池中的message),尽量不要使用objet参数的对象,而使用rFilter去识别名字和标签,只要在rFilter参数传入对象的名字或者标签就可以了,避免一次内部的转换。同理AddListener()也是一样的。若是有需要的话可自定义删除一些代码,比如不需要mListenerAdds这类列表,直接使用rImmediate为true添加到mMessageHandlers集合中处理就好。

    可改进的地方:可以使用异步委托来避免阻塞主线程。

    这个插件到这里就结束了,还是写得蛮清楚的一个插件,从中也可以得到很好学习,再见!

  • 相关阅读:
    搭建Windows下基于Eclipse 的 PHP 开发环境
    Nuit
    typedef 用法小结
    什么是 GUID?
    软件啊
    C++ 学习经典
    [转载]OGRE初学者引导
    [转载]编程的首要原则(s)是什么?
    [转载]怎样成为优秀的软件模型设计者?
    [转载]管理角度看C++游戏程序员发展
  • 原文地址:https://www.cnblogs.com/SeaSwallow/p/6537651.html
Copyright © 2011-2022 走看看