Adapter
// 需要转换成的接口定义
public interface ITarget {
void f1();
void fc();
}
// 不兼容 ITarget 接口定义的接口
public class Adaptee {
public void fa() { //... }
public void fc() { //... }
}
// 类适配器: 基于继承
public class Adaptor extends Adaptee implements ITarget {
public void f1() {
super.fa();
}
// fc()不需要实现,直接继承自Adaptee,这是跟对象适配器最大的不同点
}
// 对象适配器:基于组合
public class Adaptor implements ITarget {
private Adaptee adaptee;
public Adaptor(Adaptee adaptee) {
this.adaptee = adaptee;
}
public void f1() {
adaptee.fa(); //委托给Adaptee
}
public void fc() {
adaptee.fc();
}
}Last updated
