Adapter Pattern

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

o o de sign.

co m

http://www.o o design.co m/adapter-pattern.html

Adapter Pattern

Motivation
T he adapter pattern is adapting between classes and objects. Like any adapter in the real world it is used to be an interf ace, a bridge between two objects. In real world we have adapters f or power supplies, adapters f or camera memory cards, and so on. Probably everyone have seen some adapters f or memory cards. If you can not plug in the camera memory in your laptop you can use and adapter. You plug the camera memory in the adapter and the adapter in to laptop slot. T hat's it, it's really simple. What about sof tware development? It's the same. Can you imagine an situation when you have some class expecting some type of object and you have an object of f ering the same f eatures, but exposing a dif f erent interf ace? Of course, you want to use both of them so you don't to implement again one of them, and you don't want to change existing classes, so why not create an adapter...

Intent
Convert the interf ace of a class into another interf ace clients expect. Adapter lets classes work together, that could not otherwise because of incompatible interf aces.

Implementation
T he f igure below shows a UML class diagram f or the Adapter Pattern:

T he classes/objects participating in adapter pattern: Target - def ines the domain-specif ic interf ace that Client uses. Adapter - adapts the interf ace Adaptee to the Target interf ace.

Adaptee - def ines an existing interf ace that needs adapting. Client - collaborates with objects conf orming to the Target interf ace.

Applicability & Examples


T he visitor pattern is used when: When you have a class(Target) that invokes methods def ined in an interf ace and you have a another class(Adapter) that doesn't implement the interf ace but implements the operations that should be invoked f rom the f irst class through the interf ace. You can change none of the existing code. T he adapter will implement the interf ace and will be the bridge between the 2 classes. When you write a class (Target) f or a generic use relying on some general interf aces and you have some implemented classes, not implementing the interf ace, that needs to be invoked by the Target class. Adapters are encountered everywhere. From real world adapters to sof tware adapters Non Software Examples of Adapter Patterns : Power Supply Adapters, card readers and adapters, ... Software Examples of Adapter Patterns : Wrappers used to adopt 3rd parties libraries and f rameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application f rom the library. If another library has to be used only an adapter f or the new library is required without having to change the application code.

Specif ic problems and implementation


Object s Adapt ers - Based on Delegat ion
Objects Adapters are the classical example of the adapter pattern. It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee). T his behaviour gives us a f ew advantages over the class adapters(however the class adapters can be implemented in languages allowing multiple inheritance). T he main advantage is that the Adapter adapts not only the Adpatee but all its subclasses. All it's subclasses with one "small" restriction: all the subclasses which don't add new methods, because the used mechanism is delegation. So f or any new method the Adapter must be changed or extended to expose the new methods as well. T he main disadvantage is that it requires to write all the code f or delegating all the necessary requests tot the Adaptee.

Class Adapt ers - Based on (Mult iple) Inherit ance

Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). T hus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition. It means that instead of delegating the calls to the Adaptee, it subclasses it. In conclusion it must subclass both the Target and the Adaptee. T here are advantages and disadvantages: It adapts the specif ic Adaptee class. T he class it extends. If that one is subclassed it can not be adapted by the existing adapter. It doesn't require all the code required f or delegation, which must be written f or an Object Adapter. If the Target is represented by an interf ace instead of a class then we can talk about "class" adapters, because we can implement as many interf aces as we want.

How Much t he Adapt er Should Do?


T his question has a really simple response: it should do how much it has to in order to adapt. It's very simple, if the Target and Adaptee are similar then the adapter has just to delegate the requests f rom the Target to the Adaptee. If Target and Adaptee are not similar, then the adapter might have to convert the data structures between those and to implement the operations required by the Target but not implemented by the Adaptee.

Two-Ways Adapt ers


T he Two-Ways Adapters are adapters that implements both interf aces of Target and Adaptee. T he adapted object can be used as Target in new systems dealing with Target classes or as Adaptee in other systems dealing with Adaptee classes. Going f urther on this line of thinking, we can have adapters implementing n interf aces, adapting to n systems. Two-way adapters and n-way adapters are hard to implement in systems not supporting multiple inheritance. If adapter has to extend the Target class it can not extent another class like Adaptee, so the Adaptee should be an interf ace and all the calls should be delegated f rom the adapter to the Adaptee object.

Adapt er Pat t ern and St rat egy Pat t ern


Adapter Pattern and Strategy Pattern - there are many cases when the adapter can play the role of the Strategy Pattern. If we have several modules implementing the same f unctionality and we wrote adapters f or them, the adapters are implementing the same interf ace. We can simply replace the adapters objects at run time because they implements the same interf ace.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy