Search code examples
design-patternssoftware-designcommand-pattern

Confused about the Command Pattern


I am currently taking a software design course. I myself have never been really good at designing software systems so I thought it was a smart move to patch up my weakness ;)

I am trying to understand how the command pattern works. I have looked all over for examples, but I am still puzzled. What are the responsibilities of each class in the pattern? Do ConcreteCommands actually perform the action or do they carry the information necessary and send it off to the appropriate destination to be actually executed?


Solution

  • Suppose you have a piece of paper with step-by-step instructions relative to some object. You put this paper into an envelope and seal it. Then you give the envelope to a friend of yours knowing that he will open it and do exactly what is written here when some event happen.

    In this case you're a Client. The paper is a ConcreteCommand. Receiver is the object you mentioned in the paper. And Invoker is your friend.

    You choose the conditions under which envelope will be opened by choosing the Invoker.