适配器模式就是在接口外再包装一层
比如说以前有一个接口用来查询项目数据,多个参数来查询项目信息
java
public interface ProjectOld {
public void getInfo(String ProName, String PriId, String ob);
}
现在有个的接口也是用来查询项目数据的,要两个参数(名称和ID)
java
public interface ProjectNew {
public void getInfo(String ProName, String PriId);
}
java
public class ProjectNewImpl implements ProjectNew {
@Override
public void getInfo(String ProName, String PriId) {
System.out.println(ProName+"项目,id:"+PriId"的项目信息为xxxxx");
}
}
现在将新的查询接口转换为系统的目标接口
java
public class ProAdapter implements ProjectOld {
private ProjectNew projectNew;
public ProAdapter(ProjectNew projectNew) {
this.projectNew = projectNew;
}
@Override
public void getInfo(String ProName, String ID,String ob) {
projectNew.getInfo(ProName, ID);
}
}
最终的时候调用可以实现原来接口和现有的接口共存
java
public class AdapterClient {
public static void main(String[] args) {
ProjectOld projectOld = new ProAdapter(new ProjectNewImpl());
projectOld.getInfo("Test", "664548","");
}
}