如果您阅读Eclipse源代码,您会发现IAdaptable是一个非常流行的接口,该接口由许多其他人实现。为什么这么多的类/接口实现IAdaptable?答案是适配器是Eclipse Core Runtime的核心设计模式。

eclipse数据库表注释(解密Eclipse体系结构IAdaptable)(1)

IAdaptable是什么?

public interface IAdaptable {

/**

* Returns an object which is an instance of the given class

* associated with this object. Returns <code>null</code> if

* no such object can be found.

*

* @param adapter the adapter class to look up

* @return a object castable to the given class,

* or <code>null</code> if this object does not

* have an adapter for the given class

*/

public Object getAdapter(Class adapter);}

IAdaptable用于Eclipse扩展支持。它将现有的类改编为另一个接口。getAdapter方法返回可转换为给定接口的对象。

值得一提的是,调整类意味着获取包装器类,该包装器类是目标的子类型。包装器类包装适配器。

为什么我们需要IAdaptable?

随着向系统中添加新功能,并且这些新功能需要现有类提供的服务,我们需要使现有服务适用于新功能(即类)。

一个典型的示例是Eclipse Properties视图。它显示了所选对象的一组属性。在“ Pacakge”视图或“ Hierarchy”视图中选择任何项目时,属性视图将显示所选项目的属性。

“属性”视图需要一个界面来获取属性,然后显示它们。该接口是IPropertySource。

org.eclipse.resources插件提供IResource,IFile,IFolder等接口。如果我们希望Property视图显示IFile的属性,一种方法是让IFile扩展IPropertySource。这将起作用,但是此解决方案存在一些问题。

公共 接口 IFile 扩展了 IPropertySource

首先,必须更改IFile对象以实现IPropertySource的方法。其次,我们可能需要IFile来实现许多其他接口,然后结果会肿。

如果IFile对象实现了IAdapter接口,则

public Object getAdapter(Class adapter){

if(adapter.equals(IPropertySource.class)){

return new PropertySourceAdapter(this);

}

return null;

}

class PropertySourceAdapter implements IPropertySource{

private final Object item;

public PropertySourceAdapter(Object item){

this.item = item;

}

//required methods of IPropertySource ...

}

这样,实现IAdaptable接口的对象可以轻松地适应任何给定的接口。

实际上,这不是eclipse中使用的解决方案,因为这需要更改IFile类。阅读IAdaptable的第3部分时,您会发现不应更改IFile。适配器在plugin.xml文件中配置。

最后,开发这么多年我也总结了一套学习Java的资料与面试题,如果你在技术上面想提升自己的话,可以关注我,私信发送领取资料或者在评论区留下自己的联系方式,有时间记得帮我点下转发让跟多的人看到哦。

eclipse数据库表注释(解密Eclipse体系结构IAdaptable)(2)

eclipse数据库表注释(解密Eclipse体系结构IAdaptable)(3)

,