【发布时间】:2011-06-10 11:35:31
【问题描述】:
在阅读Head First Design Patterns 中的类适配器模式时,我遇到了这句话:
类适配器...因为你需要多重继承来实现它,这在Java中是不可能的
只是为了实验,我尝试了以下方法:
interface MyNeededInterface{
public void operationOne(MyNeededInterface other);
public MyNeededInterface operationTwo();
}
public class ThirdPartyLibraryClass{
public void thirdPartyOp();
}
假设我创建:
class ThirdPartyWrapper extends ThirdPartyLibraryClass implements MyNeededInterface{
@Override
public void operationOne(ThirdPartyWrapper other){
this.thirdPartyOp();
dosomeExtra();
}
@Override
public ThirdPartyWrapper operationTwo(){
int somevalue = doSomeThingElse();
return new ThirdPartyWrapper(somevalue);
}
}
在我的代码中,我可以使用:
MyNeededInterface myclass = createThirdPartyWrapper();
myclass.operationOne(someobj);
...
这不是类适配器模式吗?
【问题讨论】:
标签: java class design-patterns adapter