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?
The main difference is , the command does some action over the object. It may change the state of an object. While Strategy decides how to process the object. It encapsulates some business logic.
In addition, Adapter changes the interface of an existing object, while the Decorator enhances an object without changing its interface. Adapter provides a different interface to the wrapped object, Decorator provides it with an enhanced interface and Proxy provides it with the same interface.
The Adapter pattern allows the interface of an existing class to be used as another interface. The Façade pattern enables an object to provide a simplified interface to a larger body of code, such as a class library.
In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With