Please enable JavaScript.
Coggle requires JavaScript to display documents.
ADAPTER PATTERN (Wrapper) - Coggle Diagram
ADAPTER PATTERN
(Wrapper)
There are 4 patterns that are very much a like and they get confused
Adapter
Is about making two interfaces that are not compatible => compatible
Interfaces compatible
Facade
Is about taking a bunch of complex interactions and creating a facade instead of dealing with all the complex interactions
Hides some complex logic
Proxy
Is a way of placing a proxy between something we want to call.
We want to call a particular thing but instead of calling that thing we call the proxy who call that thing
The reason could be security or caching ecc...
Intercept a call and controls actions to the underlaying object
Decorator
Is a way of adding behaviour to some particolar object without opening and changing the object
Adds behavior to a particular object
Definition
The adapter pattern converts the interface of a class into another interface that client expects. Adapter let's classes work together that couldn't otherwise because of incompatible interfaces.
The intention is to not changing the underlaying behavior. We adapt something. The intention is not to add, alter, remove behavior
UML
Client
ITarget
Request()
Adapter
(implements ITarget)
Request()
Adaptee
SpecificRequest()
When we call Request the adapter delegates the request to the adaptee
Concrete
Request is the standard that we want to use
The client want's to call SpecificRequest() but for some reason he want's to call it with the Request() signature
EXAMPLE
For example the ITarget standard is scattered in different places and we don't want to change it
Or in the future we are not sure if we want to use the SpecificAdapter
Basics
The client has an ITarget
The ITarget has a method called Request()
The Request is the standard we used to use
But since we want to use a SpecificRequest()
We invoke the request method on an Adapter
The adapter has an Adaptee so will call the SpecificRequest(), he will delegate the request
The adaptee will perform the specific action
CODE
class Client
ITarget target = new Adapter(new Adaptee())
target.Request()
interface ITarget
void Request()
class Adapter : ITarget
Adaptee adaptee;
// Initialize with constructor
private Adapter(Adaptee a)
{adaptee = a}
void Request()
{adaptee.specReqeust()}
class Adaptee
public void specRequest()
Could be an external library
Something we are traying to deprecate