0

What are some example use cases where the command pattern may be of use? I've been reading about it, and I have a good idea on how to implement it. But It's a big vague when it comes to knowing when to use it.

One of the problems it solves is to decouple the API of a class. But what does that imply exactly? What's the disadvantage of calling an object's methods directly?

What other problems does it solve, and how do they benefit from it?

user1164937
  • 1,979
  • 2
  • 21
  • 29
  • Wikipedia has comprehensive coverage on the topic - https://en.wikipedia.org/wiki/Command_pattern. If you have to change the methods of an object, and those methods are called directly throughout a software project then the project has to be updated accordingly wherever these direct calls are made. It's a maintenance disadvantage. Decoupling prevents this type of maintenance overhead. – ThisClark Aug 31 '16 at 13:34
  • @ThisClark The wiki is one of the first places I went to, but most of the content (implementation, terminology, etc.) aren't very helpful except for the intro. As per the maintenance disadvantage, I don't quite understand. Could you give a concrete example please? – user1164937 Aug 31 '16 at 13:44
  • 1
    Possible duplicate of [Why should I use the command design pattern while I can easily call required methods?](http://stackoverflow.com/questions/32597736/why-should-i-use-the-command-design-pattern-while-i-can-easily-call-required-met) – jaco0646 Aug 31 '16 at 15:03
  • @jaco0646 It does seem a bit misleading, but I meant to use that as an example. Furthermore, I was hoping someone could expand upon it. I realize there are others I may have missed. – user1164937 Aug 31 '16 at 15:18

2 Answers2

3

Command pattern is useful when you need to treat an action as an object, so that you can:

  • add new action types withouth switch case construct (read: plugin)
  • store the action to be exectute later (eg., active object)
  • associate to the action complementary info (e.g., how to undo the action)

A simple example of command pattern in real code is the following.

When implementing a protocol, you can use the command pattern to decouple the parsing of a byte stream from the action associated to the message (remember that in OO you don't manage messages, but messages manage themselves :-)

When the channel receives some byte from the wire, it constructs the right command object (e.g., using the prototype or abstract factory pattern) and put it in a queue to be executed later (possibly in another thread).

Daniele Pallastrelli
  • 2,430
  • 1
  • 21
  • 37
0

The command pattern can help you in terms of extensibility as you can add a new use cases without changing the existing code.

It also permits you to chain commands and execute it in sequential or parallel mode (dependings of the language capabilities) and create macros.

You can see more examples in this page: http://gameprogrammingpatterns.com/command.html

Dario
  • 3,905
  • 2
  • 13
  • 27