1.老的接口
package adapter;
public interface OldInterface {
/**
* 老的计划
*/
public void oldPlan();
}
2.老的接口实现类
package adapter;
public class OldImp implements OldInterface{
@Override
public void oldPlan() {
System.err.println("这是老的计划!");
}
}
3.新的接口
package adapter;
public interface NewInterface {
/**
* 新的计划
*/
public void newPlan();
}
4.新的接口实现类
package adapter;
public class NewImp implements NewInterface {
@Override
public void newPlan() {
System.err.println("这是新的计划!");
}
}
5.适配器
package adapter;
public class Adapter implements OldInterface {
private NewInterface newImp;
public Adapter (NewInterface newImp){
this.newImp = newImp;
}
@Override
public void oldPlan() {
newImp.newPlan();
}
}
6.测试
package adapter;
public class demo {
public static void main(String[] args) {
NewInterface newi = new NewImp();
Adapter ada = new Adapter(newi);
ada.oldPlan();
}
}
7.结果,调用老的方法实现新的计划