Skip to content
October 8, 2008 / Shrikant Patil

Structural Pattern: Adapter Design Pattern

This is another pattern which lies in structural Design Pattern section. To understand the pattern, let’s assume that there are two persons, one person can understand English language only and another can understand Spanish language only. But the both persons need discuss about an application. Here both persons have different interfaces of language they can understand i.e incompatible interfaces, but the requirement is that they need to discuss on an application issue. There is a solution, that is the any one of two person need to learn others language, but it takes long time to learn a new language, so we don’t want go with that solution. The problem can be solved by adapting a third person who can understand both English and Spanish languages, so that he can act as a translator between two peoples.

With perspective of oops, assume that we want to create a functionality which create a communication between two objects with incompatible interfaces, and we don’t want to make any code changes in any of the classes. Here is where the third class comes which can understand both interfaces and which can translate the request from one object to that of another class can proceed. The third class is known as “Adapter”. Let us try to understand it by another simple example,

Here we have two objects, 1) Diamond and 2) Circle, which are incompatible interfaces to communicate with each other. Here Diamond object is known as “Target” and circle object is know and “Adaptee”. Diamond doesn’t know what is radius and circle doesn’t know what sides are; they can’t interact with each other because they have different implementations. So the adapter is acts as mid-person between the Diamond and Circle, Adapter knows both interfaces of Diamond and circle. So what Diamond sends a request, Adapter knows what exactly the Diamond object is requesting about and it routs that request to the circle in that manner where circle can understand the request and act accordingly. So that there is not need to write new code for either Diamond or Circle. This is what the theory of the Adapter patter.

Here is the theoretical definition for Adapter Pattern:
It converts the interface of a class into another interface the client expects, Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces”.

Let us dig into deeper; the pattern has two types.
1) Object Adapter and
2) Class Adapter.

1) Object Adapter: Object Adapter uses composition to access and exiting class (Adaptee). The Adapter holds a reference to an instance of the exiting class (Adaptee) to access its properties and methods. Object Adapter implements the interface of Target.

  • Object Adapter implements the Target (IDiamond) interface.
  • Object Adapter stores a reference instance of Adaptee (Here in out example, Adapter Class declared a private variable “adp” which is get of the type Circle. The Circle is get created in constructor of the Adapter class).
  • Adapter uses the reference instance of Adaptee call its related method.(in our case when request() method of Adapter is called) Adapter calls the RespondMethod() of adaptee using the “adp” instance of Adaptee.

Here is a sample class code of Adapter of the above example;


package flexScript
{
    //Adapter implements Target interface (IDiamond)
    public class Adapter implements IDiamond
    {
        //Adapter holds a reference instane of Adaptee(Circle)
        private var adp:Circle;
        public function Adapter()
        {
            //create an instance of Adaptee (Circle) and asign  
            //it to the reference variable
            adp = new Circle();
        }
        //implementation of request method of Target interface(IDiamond)
        public function request():void{
            //call the related method on Adaptee (Circle)
            adp.RespondMethod();
        }

    }
}

Here is the Main Application file which is using the Object Adapter ;


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="init()">
<mx:Script>
    <!&#91;CDATA&#91;
        import flexScript.*;

        private function init():void{
            var target:IDiamond = new Adapter();
            target.request();    
        }
    &#93;&#93;>
</mx:Script>
</mx:Application>

Note that, in init() method, we are creating the instance of the Adapter class and calls the request() method defined in IDiamond interface. The client doesn’t know that Adapter is communicating with Circle (Adaptee) class.

2) Class Adapter: Class Adapter uses inheritance to access and exiting Adaptee (Circle) class. Class Adapter class extends the exiting Adaptee (Circle) class to access its properties and methods. This inheritance allows to inherit properties and methods of Adaptee (circle) class.

  • Class Adapter implements the Target (IDiamond) interface.
  • Class Adapter Extends Adaptee (Circle) Class.
  • When Target class a method, Adapter uses the reference instance of Adaptee call its related method.(in our case when request() method of Adapter is called, Adapter calls the RespondMethod() of adaptee using the “adp” instance of Adaptee.
  • When an request is made from client to Adapter, Adapter calls the related method of Adaptee directly. Because it extends the Adaptee Class, all public methods are inherited by the Adaptee (Circle) class.

Here is sample of Class Adapter class code;


package flexScript
{
    //Adapter implements Target interface (IDiamond)
    // and extends the Adaptee (Circle)
    public class Adapter extends Circle implements IDiamond
    {
        //implementation of request method of Target interface(IDiamond)
        public function request():void{
            //call the related method on Adaptee (Circle)
            //inherited method of Adaptee (Ciecle)
            RespondMethod();
        }

    }
}

The Adapter is intended to provide a way for a client to use an object whose interface is different from the one expected by the client, without having to modify either. This pattern is suitable for solving issues that arise, for example, when:
1) you want to replace one class with another and the interfaces do not match, and
2) you want to create a class that can interact with other classes without knowing their interfaces at design time.

I will try to create and put a real sample application using Adapter Pattern in coming posts.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: