Adapter Pattern
From Logic Wiki
Also known as wrapper
Like UK plug to EU plug
Contents
Video
https://www.youtube.com/watch?v=2PKQtcJjYvc
Definition
The adapter pattern, converts the interface of a class into another interface the client accepts.
Code
Interface
interface ITarget{
void request();
}
Concrete Adapter
class Adapter:ITarget{
private Adaptee adaptee;
public Adapter (Adaptee a ){
this.adaptee = a;
}
public void request(){
this.adaptee.SpecificRequest()
}
}
Concrete Adaptee
class Adaptee{
public void specificRequest(){
.....
}
}
Client
ITarget target = new Adapter(new Adaptee());
Another Definition
An object that fits another object in a given interface
Adapter design pattern is used when we have an object that contains functionality which we need but not in the way we want. An adapter helps us bridge the provided and the desired functionality.
Another Example
public interface Animal{
public void move();
}
public class Lion implements Animal{
public void move(){...}
}
public class TigerToy(){
public void roll(){...}
}
public class TigerToyAdapter(){
TigerToy tiger;
public TigerToyAdapter(TigerToy tiger){
this.tiger = tiger;
}
public void move(){
tiger.roll();
}
}
class Main{
public static void main(String args[]){
Lion lion = new Lion();
TigerToyAdapter tiger= new TigerToyAdapter(new TigerToy());
lion.move();
tiger.move();
}
}
