Suggested Pages

Friday, July 27, 2012

Design Pattern Adapter

In the next days i'm going to talk about the most important design patterns used in java programs.
In this post i'll show the design pattern Adapter that is a structural design pattern as Bridge and Composite.

Problem


The design pattern Adapter faces the problem of transforming an "incompatible" interface of an object into another interface expected by the client.

Solution


The design pattern Adapter suggests to resolve the problem by giving to the object with the incompatible interface an interface as the client expects.

Components


  • Target: it defines an interface into the application domain and it's a possible candidate to be called by a client;
  • Adapter: it adapts the interface of Adaptee object to the Target interface;
  • Adaptee: it defines an existing interface that needs to be adapted;
  • Client: communicates with the objects that adhere with the Target interface .


Class Adapter


The Adapter class becomes a subclass of the Adaptee class and it implements the Target interface.
At this way the client can invoke a method of the Adapter class because it adheres with the Target interface and it preserves the functionalities of the existing Adaptee class.

Object Adapter


UML Rappresentation



Object Adapter


The Adapter object has a reference to the Adaptee object and it implements the Target interface.
When a client invokes a method of the Adapter interface it delegates the Adaptee a part or all of the responsibility to serve the request method.

UML Rappresentation


Consideration


Generally the design pattern Adapter is used when you're interested into exposing to a client a functionality that you know to be present in your application domain. Unfortunately the object that exposes this functionality has an incompatible interface so it cannot be called by the client.
So you have to convert the incompatible interface of your object into another interface known by the client.

No comments :

Post a Comment

Suggested Pages