简体   繁体   English

模式-事件分派器是否没有其他内容?

[英]Patterns - Event Dispatcher without else if?

I'm creating a Python wrapper for the Detours library. 我正在为Detours库创建一个Python包装器。 One piece of the tool is a dispatcher to send all of the hooked API calls to various handlers. 该工具之一是调度程序,用于将所有已挂接的API调用发送到各种处理程序。

Right now my code looks like this: 现在,我的代码如下所示:

if event == 'CreateWindowExW':
    # do something
elif event == 'CreateProcessW':
    # do something
elif ...

This feels ugly. 这感觉很难看。 Is there a pattern to create an event dispatcher without my having to create an elif branch for each Windows API function? 是否有一种模式可以创建事件调度程序,而不必为每个Windows API函数创建一个elif分支?

One nice way to do this is to define a class which has methods equating to the relevant API function names, plus a dispatch method which dispatches to the correct method. 一种不错的方法是定义一个类,该类的方法等于相关的API函数名称,再加上一个调度方法,该方法将调度为正确的方法。 For example: 例如:

class ApiDispatcher(object):

    def handle_CreateWindowExW(self):
        # do whatever

    def handle_CreateProcessW(self):
        # do this one

    def dispatch(self, event):
        method = getattr(self, 'handle_%s' % event)
        method()

Those if's will eventually have to go somewhere. 那些if最终将不得不去某个地方。 Why not do it like this: 为什么不这样做:

handler = get_handler(event)
handler.process()

and in the get_handler you'd have your ifs, each returning an object which does its work in the process method. get_handler您将拥有get_handler ,每个get_handler返回一个在process方法中完成其工作的对象。

An alternative would be a map to callables, like this: 一种替代方法是映射到可调用对象,如下所示:

def react_to_create_window_exw():
   # do something with event here
   pass

handlers = {
  "CreateWindowExW" : react_to_create_window_exw
}

and you would use it like this: 并且您可以这样使用它:

handler = handlers[event]
handler()

This way you would not use any if/else conditions. 这样,您就不会使用任何if / else条件。

You can use the dispatch dict method. 您可以使用dispatch dict方法。

def handle_CreateWindowExW():
    print "CreateWindowExW"     
    #do something

events = {
    "CreateWindowExW": handle_CreateWindowExW
}

events[event]()

This way, you can just add events without having to add different if statements. 这样,您可以添加事件,而不必添加其他if语句。

Usually in such cases when you have a predefined list of actions to take, use a map eg 通常,在这种情况下,如果您有要执行的预定义操作列表,请使用地图,例如

def CreateWindowExW():
    print 'CreateWindowExW'

def CreateProcessW():
    print 'CreateProcessW'

action_map = {
    'CreateWindowExW': CreateWindowExW,
    'CreateProcessW': CreateProcessW
}

for action in ['CreateWindowExW', 'UnkownAction']:
    try:
        action_map[action]()
    except KeyError:
        print action, "Not Found"

Output: 输出:

CreateWindowExW
UnkownAction Not Found

so using a map you can create a very powerful dispatcher 因此,使用地图可以创建功能非常强大的调度程序

I didn't find anything that was as graceful as it could be in this area, so I wrote something that let's you do: 我没有找到任何在该区域可能如此优美的东西 ,所以我写了一些让您做的事:

from switcheroo import Switch, default

switch = Switch({
    'foo': lambda x: x+1,
    default: lambda x: x-1,
})

>>> switch['foo'](1)
2
>>> switch['bar'](1)
0

There are some other flavours; 还有其他味道; docs are here , code is on github , package is on pypi . 文档在这里 ,代码在github上 ,程序包在pypi上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM