ADAPTER
ADAPTER
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
interface Bird
{
// birds implement Bird interface that allows
// them to fly and make sounds adaptee
interface
public void fly();
public void makeSound();
}
interface ToyDuck
{
// target interface
// toyducks dont fly they just make
// squeaking sound
public void squeak();
}
class Main
{
public static void main(String args[])
{
Sparrow sparrow = new Sparrow();
ToyDuck toyDuck = new PlasticToyDuck();
System.out.println("Sparrow...");
sparrow.fly();
sparrow.makeSound();
System.out.println("ToyDuck...");
toyDuck.squeak();
Output:
Sparrow...
Flying
Chirp Chirp
ToyDuck...
Squeak
BirdAdapter...
Chirp Chirp
Explanation :