tags:

views:

229

answers:

8

Wether you call it Addons, Plugins or extra peices of code that is connected with the original software later, it really doesn't matter. I would love to understand how they work, there has to be a simple explanation of how to design a Plugin System. Unfortunately, I never understood it, and there remains a lot of open question in my mind. For example, how does the program find a plugin? how does it interface with it? when is it preferable for a software to have Plugin System?


Thanks for all helpful answers. It seems I asked too open question, fortunately I got keywords to look for. I liked David answer though I am not a Java guy, but his talk made sense to me :)

+7  A: 
  1. Create an interface that all plugins of a particular type will implement
  2. Write the code that will 'consume' the plugin against the interface only.
  3. Have a dynamic way to load a DLL containing the plugin type that implements your interface (for instance, have a configurable folder location to test whether any DLLs in that folder contain any types that implement your interface, and dynamically load any that do. In .NET this might use Assembly.LoadFile())

If you want to have a look at some source code, Paint.NET is free and open source, and has a plugin architecture.

Mitch Wheat
Would the downvoter please leave a comment as to why. Thanks.
Mitch Wheat
This explains how to make plugins not what's the idea behind them.
RCIX
Good answer! However, I find step 1 and 3 of this answer very clear. Could you please elaborate on step 2 "Write the code that will 'consume' the plugin against the interface only." ? I did not quite get that one. Thanx!
Clean
+1  A: 

A program typically has to be designed to look for a plug-in, and the plug-in has to have a standard access point to accept control from the main program. Every application or website does it a little differently.

The simplest type of plug-in is accessed something like this:

if (a plug-in exists/is configured)
  call predefined plug-in code

In this case, the main program is coded to only handle a specific set of plug-ins (many php-based wordpress templates are like this). A slightly more advanced plug-in

perform application specific logic
if any plug-in exists that exposes the run_after_app_specific_logic function
  call plug-in code

This second case can handle ridiculously complex plug-ins ... the plug-in would just need to implement more functions called by the master program.

Jess
The complexity of the plugin has nothing to do with the architecture to implement a plugin.
Mitch Wheat
A: 

A plugin system can be implemented in many ways, but the common way for a lot of C/C++ applications is a DLL-based plugin SDK.

The DLL will expose various automated function calls which may allow the plugin to "set itself up" in the running application such as adding menu items, new functionality or extra options for systems (like 3D rendering implementations).

Nick Bedford
A: 

More ofthen there's no need for any special discovery - the plugin mechanizm is generally dumb: Here's a code signature I understand, and here's a call(s) I can make. I have no clue how the thing I'm calling will do the job, but I expect result to be in certain format. And that is pretty much a contract. Now - the plugin will implement the contract and make itself available. In Java, for example "make available" simply means that implementing classes are loaded into memory. JDBC driver for a particular database would be a good example.

DroidIn.net
+1  A: 

Eclipse in an example of a application-framework which is entirely plugin-based, meaning that all functionality is implemented as plugins. There is a thin layer at the bottom for startup/shutdown and plugin-management, but everything else is implemented as plugins on top of that. This results in a framework which can be used for just about everything. More info about Eclipse plugin architecture can be found here: http://www.eclipse.org/articles/Article-Plug-in-architecture/plugin%5Farchitecture.html.

JesperE
+5  A: 

Plug-ins work by conforming to well-known interfaces that the main application expects to work with.

There are several ways in which a plug-in architecture actually works, but in general, these are the steps:

  1. Plug-ins are designed to match an interface that the application expects. For example, a simple application might require that plug-ins implement a IPlugin interface.
  2. Plug-ins are loaded by the application, usually when the app is starting up
  3. Plug-ins are often provided access to much of the data that the application manages. For example, Firefox plug-ins can access the current web page, and Eclipse plug-ins can access the open files.

Here are two ways (out of several) in which an application can find plug-ins:

  1. The plug-ins are known to exist in a particular folder, and the application knows to load plug-ins from that folder
  2. Each plug-in runs as a service, and the services are designed to work together (this is how an OSGi-based application works)

When plug-ins are found, they are loaded by the application (sometimes the job of a Class Loader).

A software architect might design a plug-in architecture when they expect that either the software provider or the user community will implement new features that were not originally part of the system. Two great examples are Eclipse and Firefox; other applications include Adobe Photoshop (for artistic techniques and graphical tools) and Winamp (for visualizations).

David
+1  A: 

It's very language dependent.

In an interpreted language it simply involves calling a file that follows a pattern.

In C it's pretty hard to do without help. In C+windows a "DLL" can be a plug-in and are often used that way.

In an OO language with reflection, you might create an object that implements an interface and load it reflectively. After it's loaded, you can ignore the fact that it was a plug-in because it's treated as any other object in your code.

.net has a plugin architecture (is it COM?) Well anyway COM can be used as (is?) a plugin system.

Your question is probably too open-ended because of all the possibilities. There is no single answer.

Bill K
+1  A: 

I've never written a plugin system. But this is how I imagine it in my head:

  • Your program has a subdirectory for plugins (e.g. "C:\Program Files\My Program Name\plugins").
  • You create plugins as DLL files and place them in the plugins folder.
  • These DLLs would export functions with predefined names.
  • When you run your program, it looks through all the DLLs in your plugins folder. In each one it would look for an exported function with a certain name (e.g. "Load") and call that function. The plugin could then do any setup that it needed to do.
  • The program would then call an exported function on the plugin with a name like "GetPluginName". The plugin would return it's name and the program could then use that name when it displays a list of plugins to the user.
  • When it comes time to invoke the plugin, the program would call another exported function (maybe "Activate") and probably pass the plugin a pointer to the data that the plugin is going to work on. The program would then do its work on the data.
  • The plugin might also export another function that the program would call to show a setup dialog where you could change the plugin options.
Slapout