Search code examples
design-patternscommand-pattern

Why Command Pattern when a client can call the method directly from reciever instance?


Recently I have come across Command Pattern. In this pattern Client is responsible for creating the instances of Reciever, ConcreteCommand and Invoker. At some point of time (Lets say that in a Button Click) Invoke method of Invoker is being called. Now the Invoke method is responsible for executing a particular operation in Reciever. But Why do I need an Invoker ( Otherwise Command Pattern) when the client can directly call the particular action using Reciever?


Solution

  • Why do I need an Invoker (Command Pattern) when the client can directly call the particular action using Reciever?

    This is the one of the main design rationales of the Command Pattern - decouple an invoker from a receiver (sender-receiver / produced-consumer).