定义:适配器模式将某个类的接口转换成客户端期望的另一个接口表示,主的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。其别名为包装器(Wrapper)。
public class AdapterTest1 {
public static void main(String[] args) {
Adaptee adaptee=new Adaptee();
Target target=new Adapter(adaptee);
target.output5v();
}
}
class Adaptee
{
public int output220()
{
return 220;
}
}
interface Target{
int output5v();
}
class Adapter implements Target
{
//对象适配器模式(组装)
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
super();
this.adaptee = adaptee;
}
@Override
public int output5v() {
// TODO Auto-generated method stub
int i=adaptee.output220();
return 5;
}
}