<p>Is there a general procedure for programming extensibility capability into your code?</p>

<p>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.</p>

<p>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?</p>

[{'answer_id': 8155, 'author': 'Lars Mæhlum', 'author_id': 960, 'author_profile': 'https://Stackoverflow.com/users/960', 'pm_score': 1, 'selected': False, 'text': '<ol>\n<li><p>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.  </p></li>\n<li><p>Make an API the writer can use to access some of the functionality in your code.  </p></li>\n</ol>\n\n<p>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.  </p>\n\n<p>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.</p>\n\n<p>Bottom line is: When you get the code to load, only your imagination should be able to stop you.<br>\nGood luck.</p>\n'}, {'answer_id': 8157, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 3, 'selected': True, 'text': '<p>This is generally something that\xa0you\'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).</p>\n\n<p>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 <code>gimme_the_interface()</code> function which could accept a structure filled with function pointers.  These function pointers would allow the DLL to make calls, register callbacks, etc.</p>\n\n<p>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.</p>\n\n<p>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>\n\n<p>P.S. If you\'d like to see a good example of a C++ plugin system, check out the <a href="http://foobar2000.com/SDK.html" rel="nofollow noreferrer">foobar2000 SDK</a>.  I haven\'t used it in quite a while, but it used to be really well done.  I assume it still is.</p>\n'}, {'answer_id': 8164, 'author': 'palmsey', 'author_id': 521, 'author_profile': 'https://Stackoverflow.com/users/521', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 8173, 'author': 'Nickolay', 'author_id': 1026, 'author_profile': 'https://Stackoverflow.com/users/1026', 'pm_score': 2, 'selected': False, 'text': "<p>I'm tempted to point you to the Design Patterns book for this generic question :p</p>\n\n<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).</p>\n\n<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 8196, 'author': 'ricree', 'author_id': 121, 'author_profile': 'https://Stackoverflow.com/users/121', 'pm_score': 1, 'selected': False, 'text': "<p>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).  </p>\n\n<p>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.</p>\n"}, {'answer_id': 96362, 'author': 'el_eduardo', 'author_id': 13469, 'author_profile': 'https://Stackoverflow.com/users/13469', 'pm_score': 2, 'selected': False, 'text': '<p>I think there are two aspects to your question: </p>\n\n<p>The design of the system to be extendable (the design patterns, inversion of control and other architectural aspects) (<a href="http://www.martinfowler.com/articles/injection.html" rel="nofollow noreferrer">http://www.martinfowler.com/articles/injection.html</a>). And, at least to me, yes these patterns/techniques are platform/language independent and can be seen as a "general procedure".</p>\n\n<p>Now, their implementation is language and platform dependend (for example in C/C++ you have the dynamic library stuff, etc.) </p>\n\n<p>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. </p>\n\n<p>In the Java world a good specification to look is OSGi (<a href="http://en.wikipedia.org/wiki/OSGi" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/OSGi</a>) with several implementations the best one IMHO being Equinox (<a href="http://www.eclipse.org/equinox/" rel="nofollow noreferrer">http://www.eclipse.org/equinox/</a>)</p>\n'}]
['https://Stackoverflow.com/questions/8140', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/277/']