0% found this document useful (0 votes)
39 views4 pages

ADAPTER

The Adapter Pattern allows classes with incompatible interfaces to work together by converting the interface of one class into an interface expected by clients. It allows existing classes to be reused as-is, while adapting their interface to allow them to work with others. The pattern involves implementing an Adapter class that acts as a bridge between the Adaptee class and Target interface expected by clients.

Uploaded by

Bipro Barai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views4 pages

ADAPTER

The Adapter Pattern allows classes with incompatible interfaces to work together by converting the interface of one class into an interface expected by clients. It allows existing classes to be reused as-is, while adapting their interface to allow them to work with others. The pattern involves implementing an Adapter class that acts as a bridge between the Adaptee class and Target interface expected by clients.

Uploaded by

Bipro Barai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Adapter Design Pattern

An Adapter Pattern says that just "converts the interface of a


class into another interface that a client wants".
In other words, to provide the interface according to client
requirement while using the services of a class with a different
interface.
The Adapter Pattern is also known as Wrapper.

Advantage of Adapter Pattern


1. It allows two or more previously incompatible objects to
interact.

2. It allows reusability of existing functionality.

When to use?
1. When an outside component provides captivating functionality
that we'd like to reuse, but it's incompatible with our current
application. A suitable Adapter can be developed to make them
compatible with each other

2. When our application is not compatible with the interface that


our client is expecting

3. When we want to reuse legacy code in our application without


making any modification in the original code
// Java implementation of Adapter pattern

interface Bird
{
// birds implement Bird interface that allows
// them to fly and make sounds adaptee
interface
public void fly();
public void makeSound();
}

class Sparrow implements Bird


{
// a concrete implementation of bird
public void fly()
{
System.out.println("Flying");
}
public void makeSound()
{
System.out.println("Chirp Chirp");
}
}

interface ToyDuck
{
// target interface
// toyducks dont fly they just make
// squeaking sound
public void squeak();
}

class PlasticToyDuck implements ToyDuck


{
public void squeak()
{
System.out.println("Squeak");
}
}

class BirdAdapter implements ToyDuck


{
// You need to implement the interface your
// client expects to use.
Bird bird;
public BirdAdapter(Bird bird)
{
// we need reference to the object we
// are adapting
this.bird = bird;
}

public void squeak()


{
// translate the methods appropriately
bird.makeSound();
}
}

class Main
{
public static void main(String args[])
{
Sparrow sparrow = new Sparrow();
ToyDuck toyDuck = new PlasticToyDuck();

// Wrap a bird in a birdAdapter so that it


// behaves like toy duck
ToyDuck birdAdapter = new
BirdAdapter(sparrow);

System.out.println("Sparrow...");
sparrow.fly();
sparrow.makeSound();

System.out.println("ToyDuck...");
toyDuck.squeak();

// toy duck behaving like a bird


System.out.println("BirdAdapter...");
birdAdapter.squeak();
}
}

Output:
Sparrow...
Flying
Chirp Chirp
ToyDuck...
Squeak
BirdAdapter...
Chirp Chirp
Explanation :

Suppose we have a bird that can makeSound(), and we have a plastic


toy duck that can squeak(). Now suppose our client changes the
requirement and he wants the toyDuck to makeSound than ?
Simple solution is that we will just change the implementation
class to the new adapter class and tell the client to pass the
instance of the bird(which wants to squeak()) to that class.
Before : ToyDuck toyDuck = new PlasticToyDuck();
After : ToyDuck toyDuck = new BirdAdapter(sparrow);
You can see that by changing just one line the toyDuck can now do
Chirp Chirp !!

You might also like