Search code examples
design-patternsadaptercommand-pattern

Is command pattern more than adapter?


According to the GoF Command Pattern UML it seems to me like ConcreteCommand just adapts Reciever to common Command (which could be) interface. I am probably hit by golden hammer, so please explain where is the difference or what did I get wrong.

Command pattern


Solution

  • A lot of these patterns do look very similar and intent does help in understanding the pattern definition. Here is my (probably incomplete) knowledge on these patterns:

    The Command pattern seperates the invoker from the operation.

    A Command may have a lifetime independent of a specific request, receiver, or invoker - it is a seperate first-class entity. It allows the ability for queuing, and executing commands at different times and allows command execution to be recorded/remembered, perhaps for logging.

    It can be used to organise UI command implementation, undo, or redo functionality,or transaction management as examples.

    The Adapter pattern is used to adapt an object to the usage of another.

    I wouldn't confuse an object that operates on another object (ConcreteCommand doing something to Receiver) with an implementation of the 'Adaptor' pattern where an Adaptor acts almost like a proxy to allow the Adaptee to work with other collaborators.