|Re: How to reinforce singleton pattern in extension point [message #630805 is a reply to message #630788]
||Tue, 05 October 2010 06:12
| Daniel KrÃ¼gler
Registered: July 2009
On 05.10.2010 03:54, Jharsseph wrote:|
> My question is related to the extension points and extension mechanism
> in RCP development, I followed the tutorial (
> http://www.vogella.de/articles/EclipseExtensionPoint/article .html)
> and successfully made my own custom extension, but now that every time
> the following code is called, a new instance is created: final Object o
> = e.createExecutableExtension("class");
> Now I wonder how to reinforce the singleton pattern in this mechanism?
Extension points don't solve this problem for you, you need to handle
that manually. Note that this can never be a real singleton in the
strict sense, because obviously the to be constructed instances must
have a public default constructor and thus anyone can create an
arbitrary number of objects of this type.
> Do I have to code my own "Manager" like class to maintain the instance,
> or is there any recommended way of doing this? Tanks in advance.
I'm still not sure how you define "singleton" here. But assuming that
you mean that your extension point machinery shall only create one
object per given user-extension, this can be easily managed by you,
because you are already iterating through all contributions. This means
that you could define a proxy object for each contribution that
contains a single handle to the to-be-constructed object. At the point,
where construction is necessary, you do that at the latest possible
point. Typically this means that you keep an IConfigurationElement
element as the factory for this object.
HTH & Greetings from Bremen,
Powered by FUDForum
. Page generated in 0.03188 seconds