简体   繁体   English

Java开发具有插件功能的应用程序

[英]Java developing an app with plugin capabilities

I'm currently working on an application in Java that contains a core set of functionality, this has to be extended for several different purposes (~10 different purposes) and as such, the best solution that came to mind was the option to add plugins to expand the functionality when needed as opposed to having a different code base for each. 我目前正在使用包含核心功能集的Java应用程序进行开发,必须将其扩展用于几个不同的目的(约10个不同的目的),因此,想到的最好的解决方案是添加插件的选项。在需要时扩展功能,而不是每个功能都有不同的代码库。

Is the best way of doing this simply creating an interface and extending that in the relevant plugins? 做到这一点的最佳方法就是简单地创建一个接口并将其扩展到相关插件中吗?

public coreCode()
{
    // Core
    doThis();
    doThat();
    // Call plugin code
    plugin.doStuff();
}

// Plugin
public interface PluginInterface()
{
    doStuff();
    ...
}

I've looked into JSF etc but due to work limitations (not spcifically authorized etc), they are sadly not a viable option. 我已经研究过JSF等,但是由于工作限制(未特别授权等),可惜它们不是可行的选择。

--EDIT-- - 编辑 -

It should roughly work like this: 它应该大致像这样工作:

purpose A, B and C require a certain set of functionality and so plugin1 is developed for them, bundled with them and then deployed. 目的A,B和C需要特定的功能集,因此为它们开发了plugin1,将它们捆绑在一起,然后进行部署。 purpose D and E require a different set so plugin2 is developed for them, bundled and deployed. 用途D和E需要不同的集合,因此为它们开发,捆绑和部署了plugin2。 etc. 等等

For plugins you could create a certain directory where class files implementing a certain interface or extending a certain abstract class are located. 对于插件,您可以创建某个目录,在该目录中可以找到实现某个接口或扩展某个抽象类的类文件。 Then you can create new instances of these classes and see if they are an instance of the abstract class/interface you want them to be, then make a certain list of them and give users the ability to enable/disable them. 然后,您可以创建这些类的新实例,并查看它们是否是您希望它们成为的抽象类/接口的实例,然后列出它们的特定列表,并使用户能够启用/禁用它们。 If they are enabled you can then cast and execute them. 如果启用了它们,则可以强制转换并执行它们。

I would suggest you to look into OSGi - a dynamic component model where applications are developed and deployed as a set of bundles/plug-ins that can be added, started, stopped and removed remotely without even requiring a reboot of the core/host application. 我建议您研究OSGi-一个动态组件模型,在该组件中,应用程序是作为一组捆绑/插件开发和部署的,可以远程添加,启动,停止和删除该捆绑/插件,而无需重新启动核心/主机应用程序。

You can model your application on the Eclipse RCP platform (one the best implementations of OSGi) and create a head-less (without any UI) core RCP application. 您可以在Eclipse RCP平台(OSGi的最佳实现之一)上对应用程序进行建模,并创建无头(没有任何UI)核心RCP应用程序。 All other application functionalities would then be developed as plug-ins that can come bundled or added later on to your core RCP application in any combination you like (or your clients may request) even after the application has gone live. 然后,所有其他应用程序功能都将作为插件开发,即使在应用程序上线后,也可以将它们捆绑或添加到您想要的任何组合(或客户可能要求的)中的核心RCP应用程序中。

If you're application would have a UI and you like the Eclipse IDE's look and functionalities like "New Project" Wizards, use of Perspectives to change the layout of Views, Workspaces, context-sensitive Help functionality and over-the-network updates through plug-in repositories etc. then it's definitely worth considering. 如果您的应用程序具有UI,并且您喜欢Eclipse IDE的外观和功能(例如“新建项目”向导),则可以使用Perspectives通过以下方式更改View,Workspaces,上下文相关的Help功能和网络更新的布局。插件存储库等,那么绝对值得考虑。 Have a look at this RCP FAQ page to see if this fits your needs. 请查看此RCP常见问题页面,看看这是否符合您的需求。

The only thing that put developers off is that it has a bit of a steep learning curve. 唯一让开发人员失望的是,它的学习曲线有些陡峭。 The dependencies are always declared declaratively with XMLs and within the code all the components are so de-coupled that you always find yourself interacting with the framework more. 依赖关系始终使用XML声明性地声明,并且在代码内所有组件都是如此分离的,以至于您总是发现自己与框架进行了更多的交互。

The use of JFace for building UI components puts a lot of stress on the separation between the Model and the View. 使用JFace构建UI组件给模型和视图之间的分离带来了很大的压力。 So, while all of this translates to good application design it doesn't allow rapid application development if one is new to the framework. 因此,尽管所有这些都转化为良好的应用程序设计,但如果是框架的新手,则不允许快速的应用程序开发。 Most of its other complexities come from the fact that RCP just has so much to offer. 它的其他大多数复杂性来自RCP可以提供这么多的事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM