通过扩展点从 Eclipse 插件返回值?

发布于 2024-12-17 09:30:03 字数 593 浏览 1 评论 0原文

我这里有一个扩展点,它指向一个接口:

public interface IModelProcessor {

    public void processModel(Object diagramModel);
}

因此,每个想要使用该扩展点的插件都必须提供一个带有 processModel(对象图模型)的 IModelProcessor。我的问题是:

现在如何从实现此扩展点的 eclipse 插件获取返回值?我当然可以使用返回值,如下所示:

public ReturnContainer processModel(Object diagramModel);

但由于并非每个插件都会提供返回值,因此我必须制定约定,在这种情况下插件应返回 null,这是我的意见相当肮脏。 你对此有何看法:

public void processModel(Object diagramModel, Observer returnObserver);

因此,每个想要传递返回值的插件都可以注册该观察者(ofc,该插件必须是可观察的)并调用其更新方法。你对此有何看法?

i've got an Extensionpoint here which points to an interface:

public interface IModelProcessor {

    public void processModel(Object diagramModel);
}

So, each plugin which wants to usw that Extensionppoint has to provide an IModelProcessor with processModel(Object diagramModel). My Question is:

How can I now get return values from eclipse plug-ins, which implement this Extensionpoint? I could of course use an return value, somethin like this:

public ReturnContainer processModel(Object diagramModel);

But since not every plug-in will deliver a return value, I'd had to make the convention, that plug-ins should return null in this case, which is in my opinion rather dirty.
What you think about that:

public void processModel(Object diagramModel, Observer returnObserver);

So, each plug-in which wants to deliver return values can register that Observer (ofc, the plug-in would have to be Observable) and call the update method on it. What you think about that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖果控 2024-12-24 09:30:04

我能想到一些选择。

向接口添加一个 getMyValue() 方法怎么样?然后它必须默认通过抽象超类返回 null。如果确实有要返回的值,该类将重载 get 方法并返回有效值。

在 Eclipse 中,以这种方式使用抽象超类来为不使用它们的子类设置方法的默认处理是很常见的。

There are some options I can think of.

How about adding a getMyValue() method to the interface. It would then have to default to returning null through an abstract superclass. In cases where you do have a value to return, the class would overload the get-method and return a valid value.

It's pretty common in Eclipse to use abstract superclasses in this way to set default handling of methods for subclasses that dont use them.

吝吻 2024-12-24 09:30:03

这实际上取决于您希望合同是什么样子。总的来说,我觉得使用返回值比使用你提到的观察者选项更直观。通常我会使用返回值,并在发生错误时抛出异常。您还可以让每个插件返回一个对象,即使该对象是“空”。从插件返回值没有什么特别的,只要所有插件都可以访问您要返回的类。

This really depends on how you want your contract to look like. In general I feel that using return values is more intuitive than using the Observer option you mentioned. Normally I would use return values, and throw exception when errors happen. You could also have every plugin return an object, even if said object is "empty". There's nothing special about returning values from plugins, as long as all plugins have access to the class which you're meant to return.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文