`

19-Command: choosing the operation at run-time

阅读更多

      A Command is a function object in its purest sense: a method that’s an object . By wrapping a method in an object, you can pass it to other methods or objects as a parameter, to tell them to perform this particular operation in the process of fulfilling your request. You could say that a Command is a messenger (because its intent and use is very straightforward) that carries behavior, rather than data.

    ......

     The primary point of Command is to allow you to hand a desired action to a method or object. In the above example, this provides a way to queue a set of actions to be performed collectively. In this case, it allows you to dynamically create new behavior, something you can normally only do by writing new code but in the above example could be done by interpreting a script (see the Interpreter pattern if what you need to do gets very complex).

     以上描述是对Command设计模式的很好阐述,正如题目中Command设计模式就是在运行中选择“operation”,只要将“opration”按照自己的需求,实现接口就可以了,达到了调用方和“operation”解耦的目的。

     具体的应用学习,在http://lgd-java2eye.iteye.com/blog/757524 中有很好的解释,这里不赘述。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics