Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design Patterns - Adapter pattern vs Decorator Pattern? [duplicate]

I have been reading about design patterns and this got me curious:

Decorator Pattern says wrap an original object and add additional features in the wrapper object. So structurally speaking - Wrappers follow decorator pattern.

Adapter pattern says changing one object by creating an instance of it and adding functionalities to it. These functionalities do not match those of the original object so we have to modify them, but we may also add our own extra methods which are not a part of the original object.

In this regard, what is the difference between Adapter and Decorator design pattern?

like image 969
Navin Israni Avatar asked Mar 11 '17 15:03

Navin Israni


People also ask

What is the difference between a decorator and Adapter design patterns?

Adapter changes the interface of an existing object, while Decorator enhances an object without changing its interface.

Do the the proxy pattern and adapter pattern have the same structure?

The main differences between Adapter and Proxy patterns are:While proxy provides the same interface, Adapter provides a different interface that's compatible with its client. Adapter pattern is used after the application components are designed so that we can use them without modifying the source code.

What is a disadvantage of the decorator pattern?

Disadvantages. High degree of flexibility. High complexity of software (especially decorator interface) Expansion of function of classes without inheritance.

How about the decorator how does it differ from both the façade and the Adapter?

Adapter uses composition to forward calls from target interface to adaptee interface, Decorator also uses same technique before adding new behavior, Facade is composed of all sub components and Proxy also use composition and delegation to forward requests.


1 Answers

Decorator, attach additional responsibilities to an object dynamically. For example adding sugar in a coffee.

Adapter, adapts interface of an existing class to another interface. For example eletrical adapter.

like image 111
dstar55 Avatar answered Sep 28 '22 20:09

dstar55