Command Pattern seems needlessly complex (what am I failing to understand?)

前端 未结 4 1844
你的背包
你的背包 2020-12-11 01:26

I\'ve read up on the Command Pattern, and I think I\'m missing something. The Command object exists to abstract away the details of the Receiver object. It seems to me tha

4条回答
  •  攒了一身酷
    2020-12-11 01:44

    Well, if you put it that way, it seems quite complex, but often a Receiver doesn't need to be an object at all. It can be little more than just a function that is executed (as an event). Also, the invoker doesn't need to be a class. It is just the thing that triggers the command. This also can be an event handler in a button.

    Even Wikipedia sums up a couple of examples where this pattern is used without actually having to implement complete separate classes for invoker and receiver. An example is a wizard dialog, where the GUI populates the command object, and a Finish button triggers it. So that GUI class (that you have anyway) is both the client and the invoker.

提交回复
热议问题