/**
* 被适配的类
*
* @author Administrator
*
*/
public class Adaptee {
public void handle() {
System.out.println("键盘打字!!");
}
}
/**
* 适配器
*
* @author Administrator
*
*/
public class Adapter implements Target {
private Adaptee adaptee;
@Override
public void handleReq() {
adaptee.handle();
}
public Adapter(Adaptee adaptee) {
super();
this.adaptee = adaptee;
}
}
/**
* 适配器
*
* @author Administrator
*
*/
public class Adapter2 extends Adaptee implements Target {
@Override
public void handleReq() {
super.handle();
}
}
/**
* 目标接口
*
* @author Administrator
*
*/
public interface Target {
public void handleReq();
}
public class Client {
public static void main(String[] args) {
Client c = new Client();
/** 适配器方式一,组合模式**/
Adaptee adaptee = new Adaptee();
Target adapter = new Adapter(adaptee);
c.testAdapter(adapter);
System.out.println();
/** 适配器方式二,继承模式**/
Target adapter2 = new Adapter2();
c.testAdapter(adapter2);
}
/**
* 某个接口或方法,必须传指定的接口
*
* @param t
*/
public void testAdapter(Target t) {
t.handleReq();
}
}