Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 347
Insignificant Person Avatar asked Feb 08 '15 09:02

Insignificant Person


People also ask

What is the difference between command and strategy pattern?

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.

What is the difference between Adapter and decorator patterns?

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.

What is the difference between Adapter and facade pattern?

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.

What is meant by Adapter pattern?

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.


1 Answers

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.

like image 104
GFranke Avatar answered Oct 03 '22 08:10

GFranke