| [news.eclipse.platform.rcp] Re: Singletons and IConfigurationElement.createExecutableExtension |
Does this make any sense, or am I missing something?
The answer to your last question is "yes" !
I posted some details replying to myself in another message, but i will give you more details on my design here.
Actually i am developing an IDE which deals with versioning. Every element in the workspace is a IVersionable object. The core plugin knows how to manage versioning information using the IVersionable interface and provides version-related operations (checkin / checkout etc.), views and editors. It does not know "what" is really the IVersionable nor how to manage the type-specific information.
This IVersionable interface is typed. Types are defined at runtime through extensions. The extension providing a given type also declares:
- Generic factories : They provide method to create new objects of the corresponding type wrapped into a IVersionable interface for common management by the core.
- Generic controllers which define the graphical editors, custom navigators (which corresponds to the type integration in the navigator part)
The core plugin knows the factory and controller interface and delegates all the type-specific operations (edition, persistency, appearance, + some custom operation). This core is a small layer on top of the RCP platform.
Therefore I need my contribution plugins to declare factories dynamically. I would very much like this factories to be global singleton. Even if having several instances seems not a problem so far, it might result in unexpected behaviour in the future.
The only working design I have found so far which can certify my factories are global singletons is to create a new IExecutableExtensionFactory class which will return the singleton in its "create" method. But it forces me to add 1 class per extension, which might be instantiated several times with a very short lifetime since I only keep references to the returned singleton...
I would appreciate your point of view on this design and your wise pieces of advice on the best may to manage those singletons.
Thank you.
Christophe.
"Paul Webster" <pwebster@xxxxxxxxxx> a écrit dans le message de news:fj1h16$8rk$1@xxxxxxxxxxxxxxxxxxxxThe common way for a plugin to provide a singleton to other plugins (say, some kind of ThemeManager) is to have it on your Activator:
public ThemeManager getThemeManager() {
if (themeManager == null) {
// create and initialize
}
return themeManager;
}
But it's a good practice not to export your Activator class (keep it internal) so then you export a public static class that can delegate:
public class MyPluginHelper { private MyPluginHelper() { }
public ThemeManager getThemeManager() { return MyActivator.getDefault().getThemeManager(); } }
The thing with IConfigurationElement#createExecutableExtension(*) is that it is a new instance. You could, of course, use it to delegate to your singletons (but the question is why):
public class ContributedClass { public ThemeManager getThemeManager() { return MyActivator.getDefault().getThemeManager(); } }
Then it doesn't matter how many instances your IConfigurationElement creates, you'll deal with one that matters.
What are you trying to do, exactly? You have an RCP app that uses singletons (like IThemeManager) that you would like contributed by another plugin at runtime?
PW
--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/workbench.htm