-- flyaflya [2005-08-04 09:30:31]

1. Command(命令)

1.1. 意图

将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。在python中叫“Command Dispatch Pattern”,也不需要将请求封装成对象,直接用字符串作命令请求。

1.2. 代码

   1 class Dispatcher:
   2 
   3     def do_get(self):
   4             print("do_get")
   5 
   6     def do_put(self):
   7             print("d0_put")
   8 
   9     def error(self):
  10             print("error")
  11 
  12     def dispatch(self, command):
  13         mname = 'do_' + command
  14         if hasattr(self, mname):
  15             method = getattr(self, mname)
  16             method()
  17         else:
  18             self.error()

1.3. 例子

   1 Dispatcher a
   2 a.dispatch("get")
   3 a.dispatch("put")

1.4. 特殊说明