How to design extensible software (plugin architec

2019-01-09 21:54发布

I need some resources that talk about how to design your software to be extensible, i.e. so that other people can write add-ons/plug-ins that adds functionality to it.

What do you recommend? Any books out there that discuss the subject?
I would prefer something that's short and to the point; a bit of theory and a bunch of concrete examples.

I'm not targeting a specific language, I want to be able to understand the core idea so that I can implement it in any language.

And for the same reason, I prefer not to do it using a framework that someone else built (unless the framework is not very high-level, i.e. doesn't hide too much), at the moment I only want to educate myself on the subject and experiment with various ways to implement it. Plus, a framework usually assumes user's knowledge about the subject.

UPDATE

I'm not asking about OOP or allowing my classes to be inherited. I'm talking about designing an application that will be deployed on a system, such that it can be extended by third-party add-ons AFTER its been deployed.

For example, Notepad++ has a plug-in architecture where you can place a .dll file in the plugins folder, and it adds functionality to the application that wasn't there, such as color-picking, or snippet insertion, or many other things (a wide range of functionality).

13条回答
Emotional °昔
2楼-- · 2019-01-09 22:36

Plugin architecture is becoming very popular for its extensibility and thus flexibility.

For c++, Apache httpd server is actually plugin based, but a concept of module is used instead. Most of apache features are implemented as modules, like cache, rewrite, load balancing, and even threading model. It is a very modular software I ever saw.

And for java, Eclipse is definitely plugin based. The core of Eclipse is an OSGI module system which manage bundles, another concept for plugin. Bundle can provide extension points on which we can build modules with less efforts. The most intricate thing in OSGI is its dynamic characteristic, which means bundles can be installed or uninstalled at runtime. No stop-the-world syndrome any more!

查看更多
倾城 Initia
3楼-- · 2019-01-09 22:38

Implement SOLID principles in your application.

1. Single responsibility principle: A class should have only a single responsibility (i.e. only one potential change in the software's specification should be able to affect the specification of the class

2.Open/closed principle: Software entities … should be open for extension, but closed for modification

3. Liskov substitution principle: Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program

4. Interface segregation principle: Many client-specific interfaces are better than one general-purpose interface

5. Dependency inversion principle: One should Depend upon Abstractions. Do not depend upon concretions

Stackoverflow questions:

Example of Single Responsibility Principle

Is the Open/Closed Principle a good idea?

What is the Liskov Substitution Principle?

Interface Segregation Principle- Program to an interface

What is the Dependency Inversion Principle and why is it important?

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-09 22:40

Well it depends on the language.

  • In C/C++ I'm pretty sure there is a loadlibrary function that allows you to open a library at runtime and invoke it's exported functions. This is typically how it's done in C/C++.
  • In .NET, there is Reflection, which is offers similar (but more broad) to loadlibrary. There is also entire libraries built on Reflection like Managed Extension Framework, or Mono.Addins that does most of the heavy lifting for you already.
  • In Java, there is also Reflection. And there is the JPF (Java Plugin Framework) which is used in stuff like Eclipse IIRC.

Depending on what language you use I could recommend some tutorial/books. I hope this was helpful.

查看更多
劳资没心,怎么记你
6楼-- · 2019-01-09 22:41

You try to reach two competing goals:

  1. The components of your software must expose a lot of themselves, so they can be reused
  2. The components of your software must expose very little of themselves, so they can be reused

Explanation: To encourage code reuse, you should be able to extend existing classes and call their methods. This isn't possible when the methods are declared "private" and the classes are "final" (and can't be extended). So to meet this goal, everything should be public and accessible. No private data or methods.

When you release the second version of your software, you will find that many of the ideas of version 1 were plain wrong. You need to change many interfaces or your code, method names, delete methods, break the API. If you do this, many people will turn away. So in order to be able to evolve your software, the components must not expose anything that is not absolutely necessary - at the cost of code reuse.

Example: I wanted to observe the position of the cursor (caret) in an SWT StyledText. The caret is not meant to be extended. If you do it, you'll find that the code contains checks like "is this class in the package org.eclipse.swt" and a lot of methods are private and final and whatnot. I had to copy about 28 classes out of SWT into my project just to implement this feature because everything is locked down.

SWT is a nice framework to use and hell to extend.

查看更多
地球回转人心会变
7楼-- · 2019-01-09 22:41

If you work with .Net, our research yielded two approaches: scripting and composition.

Scripting

You extend the functionality of what your classes can do by orchestrating them using scripts. That means exposing what is compiled in your favorite .Net language in a dynamic language.

Some options we found worth exploring:

Composition

If you start a project with .Net 4 or above, you must take a good look at the Managed Extensibility Framework (MEF). It allows you to extend the functionality of your apps in a plugin way.

The Managed Extensibility Framework (MEF) is a composition layer for .NET that improves the flexibility, maintainability and testability of large applications. MEF can be used for third-party plugin extensibility, or it can bring the benefits of a loosely-coupled plugin-like architecture to regular applications.

Managed Add-in Framework is also a good read.

查看更多
登录 后发表回答