添加插件功能的建议?

发布于 2024-07-05 12:07:47 字数 154 浏览 8 评论 0原文

是否有将可扩展性功能编程到代码中的通用过程?

我想知道向您正在编写的系统添加扩展类型功能的一般过程是什么,以便可以通过某种插件 API 来扩展功能,而不必修改系统的核心代码。

这些事情是否往往取决于系统编写的语言,或者是否有一个通用的方法可以实现这一点?

Is there a general procedure for programming extensibility capability into your code?

I am wondering what the general procedure is for adding extension-type capability to a system you are writing so that functionality can be extended through some kind of plugin API rather than having to modify the core code of a system.

Do such things tend to be dependent on the language the system was written in, or is there a general method for allowing for this?

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

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

发布评论

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

评论(6

不必在意 2024-07-12 12:07:48

这通常是您必须公开的东西,所以是的,它将取决于您的系统编写的语言(尽管通常也可以为其他语言编写包装器)。

例如,如果您有一个用 C 语言编写的 Windows 程序,则插件将以 DLL 的形式为您的程序编写。 在运行时,您将手动加载这些 DLL,并向它们公开一些接口。 例如,DLL 可能会公开一个 gimme_the_interface() 函数,该函数可以接受填充函数指针的结构。 这些函数指针将允许 DLL 进行调用、注册回调等。

如果您使用 C++,您将使用 DLL 系统,除非您可能会传递对象指针而不是结构,并且该对象将实现一个接口,该接口提供的功能(完成与结构相同的事情,但不那么难看)。 对于 Java,您可以按需加载类文件而不是 DLL,但基本思想是相同的。

在所有情况下,您都需要在代码和插件之间定义一个标准接口,以便您可以初始化插件,以便插件可以与您交互。

PS 如果您想查看 C++ 插件系统的好示例,请查看 foobar2000 SDK 。 我已经有一段时间没有使用它了,但它曾经做得非常好。 我想现在仍然如此。

This is generally something that you'll have to expose yourself, so yes, it will be dependent on the language your system is written in (though often it's possible to write wrappers for other languages as well).

If, for example, you had a program written in C, for Windows, plugins would be written for your program as DLLs. At runtime, you would manually load these DLLs, and expose some interface to them. For example, the DLLs might expose a gimme_the_interface() function which could accept a structure filled with function pointers. These function pointers would allow the DLL to make calls, register callbacks, etc.

If you were in C++, you would use the DLL system, except you would probably pass an object pointer instead of a struct, and the object would implement an interface which provided functionality (accomplishing the same thing as the struct, but less ugly). For Java, you would load class files on-demand instead of DLLs, but the basic idea would be the same.

In all cases, you'll need to define a standard interface between your code and the plugins, so that you can initialize the plugins, and so the plugins can interact with you.

P.S. If you'd like to see a good example of a C++ plugin system, check out the foobar2000 SDK. I haven't used it in quite a while, but it used to be really well done. I assume it still is.

云醉月微眠 2024-07-12 12:07:48

对于这个一般性问题,我很想向您推荐《设计模式》一书:p

说真的,我认为答案是否定的。 默认情况下你无法编写可扩展的代码,它既难以编写/扩展,又非常低效(Mozilla 最初的想法是高度可扩展,到处都使用 XPCOM,现在他们意识到这是一个错误,并开始删除它这是没有意义的地方)。

有意义的做法是确定系统中可以进行有意义扩展的部分,并为这些情况支持适当的 API(例如编辑器中的语言支持插件)。 您将使用相关模式,但具体实现取决于您的平台/语言选择。

IMO,它还有助于使用动态语言 - 使得可以在运行时(绝对必要时)调整核心代码。 我很欣赏 Mozilla 在编写 Firefox 扩展时的可扩展性。

I'm tempted to point you to the Design Patterns book for this generic question :p

Seriously, I think the answer is no. You can't write extensible code by default, it will be both hard to write/extend and awfully inefficient (Mozilla started with the idea of being very extensible, used XPCOM everywhere, and now they realized it was a mistake and started to remove it where it doesn't make sense).

what makes sense to do is to identify the pieces of your system that can be meaningfully extended and support a proper API for these cases (e.g. language support plug-ins in an editor). You'd use the relevant patterns, but the specific implementation depends on your platform/language choice.

IMO, it also helps to use a dynamic language - makes it possible to tweak the core code at run time (when absolutely necessary). I appreciated that Mozilla's extensibility works that way when writing Firefox extensions.

停顿的约定 2024-07-12 12:07:48
  1. 了解您对插件编写者的最低要求。 然后创建一个或多个接口,编写者必须为您的代码实现这些接口,以便知道何时何地执行代码。

  2. 创建一个 API,编写者可以使用它来访问代码中的某些功能。

您还可以创建一个编写者必须继承的基类。 这将使连接 API 变得更加容易。 然后使用某种反射来扫描目录,并加载您找到的符合您要求的类。

有些人还为他们的系统制作脚本语言,或者为现有语言的子集实现解释器。 这也是一条可以走的路。

底线是:当您加载代码时,只有您的想象力才能阻止您。
祝你好运。

  1. Find out what minimum requrements you want to put on a plugin writer. Then make one or more Interfaces that the writer must implement for your code to know when and where to execute the code.

  2. Make an API the writer can use to access some of the functionality in your code.

You could also make a base class the writer must inherit. This will make wiring up the API easier. Then use some kind of reflection to scan a directory, and load the classes you find that matches your requirements.

Some people also make a scripting language for their system, or implements an interpreter for a subset of an existing language. This is also a possible route to go.

Bottom line is: When you get the code to load, only your imagination should be able to stop you.
Good luck.

我的鱼塘能养鲲 2024-07-12 12:07:48

我认为你的问题有两个方面:

系统的设计是可扩展的(设计模式、控制反转和其他架构方面)(http://www.martinfowler.com/articles/injection.html)。 而且,至少对我来说,是的,这些模式/技术是独立于平台/语言的,可以被视为“通用过程”。

现在,它们的实现取决于语言和平台(例如,在 C/C++ 中,您拥有动态库等)。

已经开发了几个“框架”来为您提供一个编程环境,为您提供可插拔性/可扩展性,但正如其他人一样提一下,不要太疯狂地让所有东西都可插入。

在 Java 世界中,一个值得关注的规范是 OSGi (http://en.wikipedia.org/wiki/ OSGi)有多种实现,恕我直言,最好的一个是 Equinox(http://www.eclipse.org /春分/)

I think there are two aspects to your question:

The design of the system to be extendable (the design patterns, inversion of control and other architectural aspects) (http://www.martinfowler.com/articles/injection.html). And, at least to me, yes these patterns/techniques are platform/language independent and can be seen as a "general procedure".

Now, their implementation is language and platform dependend (for example in C/C++ you have the dynamic library stuff, etc.)

Several 'frameworks' have been developed to give you a programming environment that provides you pluggability/extensibility but as some other people mention, don't get too crazy making everything pluggable.

In the Java world a good specification to look is OSGi (http://en.wikipedia.org/wiki/OSGi) with several implementations the best one IMHO being Equinox (http://www.eclipse.org/equinox/)

帅气称霸 2024-07-12 12:07:48

如果您使用的是 C 或 C++ 等编译语言,那么通过脚本语言查看插件支持可能是个好主意。 Python 和 Lua 都是优秀的语言,可用于编写大量应用程序的脚本(Civ4 和 Blender 使用 Python,Supreme Commander 使用 Lua 等)。

如果您使用 C++,请查看 boost python 库。 除此之外,Python 附带了可在 C 中使用的标头,并且在记录 C/Python API 方面做得相当好。 Lua 的文档似乎不太完整,但我可能还不够努力。 无论哪种方式,您都可以提供一个相当可靠的脚本平台,而无需进行大量的工作。 它仍然不是微不足道的,但它为您提供了一个非常好的工作基础。

If you are using a compiled language such as C or C++, it may be a good idea to look at plugin support via scripting languages. Both Python and Lua are excellent languages that are used to script a large number of applications (Civ4 and blender use Python, Supreme Commander uses Lua, etc).

If you are using C++, check out the boost python library. Otherwise, python ships with headers that can be used in C, and does a fairly good job documenting the C/python API. The documentation seemed less complete for Lua, but I may not have been looking hard enough. Either way, you can offer a fairly solid scripting platform without a terrible amount of work. It still isn't trivial, but it provides you with a very good base to work from.

天邊彩虹 2024-07-12 12:07:47

我过去曾在插件中使用基于事件的 API。 您可以通过分派事件并提供对应用程序状态的访问来插入插件挂钩。

例如,如果您正在编写博客应用程序,您可能希望在将新帖子保存到数据库之前引发一个事件,并将帖子 HTML 提供给插件以根据需要进行更改。

I've used event-based APIs for plugins in the past. You can insert hooks for plugins by dispatching events and providing access to the application state.

For example, if you were writing a blogging application, you might want to raise an event just before a new post is saved to the database, and provide the post HTML to the plugin to alter as needed.

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