singleton problem makes class hard to test

M

mike

Hi,

I have a class called MyPlugin that extends Plugin ( Eclipse run-time
class instantiated by the framework).
In MyPlugin I call another class, MyProvider, that provides a
singleton e.g. I get an instance using the following code:
MyProvider.getInstance(). This is what MyPlugin class uses to get an
instance of MyProvider. I don't see how I can get a looser coupling
since the MyPlugin constructor is called form the eclipse framework.
It is not possible to get an instance of MyProvider into the MyPlugin
constructor.

Have I missed something? Is there a way around this?

br,

//mikael
 
M

Mark Space

mike said:
Hi,

I have a class called MyPlugin that extends Plugin ( Eclipse run-time
class instantiated by the framework).
In MyPlugin I call another class, MyProvider, that provides a
singleton e.g. I get an instance using the following code:
MyProvider.getInstance(). This is what MyPlugin class uses to get an
instance of MyProvider. I don't see how I can get a looser coupling
since the MyPlugin constructor is called form the eclipse framework.

What's the type of MyProvider? Can you make one of those?

If Eclipse declares some type like

package org.eclipse;
interface ProviderFactory {}

Can you make your own factor?

package you.test;
public class TestProvider implements ProviderFactory {
public Provider getInstance() { /*...*/ } // static?
//...
}

If getInstance is a static method, then it can't be a method in the
interface, but I didn't show one there either. Eclipse might poke the
provider object reflexively and just use ProviderFactory as a marker
interface.

Once you have a ProviderFactory of your own, you can inject it into your
components for testing.
 
D

Daniel Pitts

mike said:
Hi,

I have a class called MyPlugin that extends Plugin ( Eclipse run-time
class instantiated by the framework).
In MyPlugin I call another class, MyProvider, that provides a
singleton e.g. I get an instance using the following code:
MyProvider.getInstance(). This is what MyPlugin class uses to get an
instance of MyProvider. I don't see how I can get a looser coupling
since the MyPlugin constructor is called form the eclipse framework.
It is not possible to get an instance of MyProvider into the MyPlugin
constructor.

Have I missed something? Is there a way around this?

br,

//mikael
class MyPlugin {
private final MyProvider provider;
public MyPlugin() {
provider = MyProvider.getInstance();
}
public MyPlugin(MyProvider provider) {
this.provider = provider;
}
}

public TestMyPlugin {
public void testSomething() {
MyPlugin plugin = new MyPlugin(new MyTestProvider());
}
}
 
Joined
Feb 11, 2009
Messages
12
Reaction score
0
Or move the getInstance call to a method and use anonymous overriding to pass a mock object. In that way also you will be able to fully unit test your code with out depending on the other class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top