0

I wonder how to design a good plugin architecture in C++/Qt. The main concern I have is about dependency injection vs globals when it comes to the core components. Plugins should have access to several core functionalities like registering a socket listener, modifiying the tray menu, have acces to the main window, starting/restarting the app, have acces to the extension pool etc...

What I wonder now is if a class PluginLoader should hold all these things and inject them in a method initialize(…………) with a huge parameter list, or if globals are 'ok' in this context, since these core components are singletons by nature anyway.

1 Answer 1

2

Anything you make available to plugins becomes part of the public API of your application and cannot be changed without big costs. With that in mind, you should try to keep your public API as small as possible, so that you don't restrict yourself in making changes to the core functionality.

From that perspective, you should not expose (the names of) global variables in your public API if there is any way to avoid it.

On the other hand, a huge initialize method that injects all possible dependencies into all plugins, whether a plugin needs them or not is also not desirable and also not future-proof.

For the plugin dependencies that are true singletons, you can use the Singleton design pattern. It is almost the same as a global variable, but clearly expresses the intent that there must only ever a single instance (no matter what requirement changes will be thought of).

For the plugin dependencies that are not singletons, I would provide a "dependency resolver" class/instance to the plugins from which they can in turn request the dependencies they need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.