Insignificant Person
Insignificant Person

Reputation: 913

Difference Between Command and Adapter Patterns

In Adapter pattern we change the interface of an object to another one to use it from another interface.

In Command pattern we change the interface of an object to a common Command interface, providing an execute method that calls the real methods which does the job. Isn't this exactly the same thing which adapter pattern already does? What's the point of Command pattern then?

Upvotes: 10

Views: 2855

Answers (1)

GFranke
GFranke

Reputation: 237

The Command design pattern is used to solve problems like:
- How can an object be configured (customized) with a request?
- And how can the request be (ex)changed dynamically at run-time?

The point of Command is to decouple a request from its invoker and encapsulate it in a separate object (Command interface).
Invoker then delegates a request to a command object dynamically.

The Adapter design pattern (object adapter) is used to solve problems like:
- How can an object be accessed that has an incompatible interface
without changing existing interfaces?

The point of Adapter is to work through a separate object that adapts an incompatible interface, i.e., that implements our needed interface (Target) in terms of (by delegating to) the incompatible interface.

The Command pattern is more similar to the Strategy pattern, which decouples an algorithm from its context and encapsulates it in a separate object (Strategy).

For further discussion see the GoF Design Patterns Memory for learning object-oriented design & programming at http://w3sdesign.com.

Upvotes: 4

Related Questions