Command Design Pattern - Is Invoker Optional?

后端 未结 2 972
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 07:19

Is Invoker class optional in Command design pattern? Client needs to instantiate Concrete Command and Receiver for the command. Does client always need to instantiate Invoker an

2条回答
  •  梦如初夏
    2021-02-14 07:50

    The purposes of Command pattern are usually 1) Make a set of different operations share the same type so they can be processed by the same code 2) separate operation marshalling/creation from operation invocation. The Reciever is explicitly required for purpose 2.

    If you invoke right after creation or if the Reciever is playing the role of invoker, there's no single-purpose, stand-alone invoker. Whether that means that there's no invoker is really a philosophical question :)

    Look at it this way: You /can/ seperate the creation, scheduling and invocation. That doesn't mean that you have to implement them as three separate classes. It's just the logical roles that are involved in the Command pattern life cycle.

    EDIT: I guess the single responsibility principle argues that you should separate them, but there's such a thing as commen sense :) Local conditions can and should be observed.

提交回复
热议问题