I want to know, how to use the singleton pattern with interfaces? I have created an interface:
public interface BusinessService {
BusinessService getInstance();
}
I make this implementation:
public class BusinessServiceImpl implements BusinessService {
private static BusinessServiceImpl instance = null;
private BusinessServiceImpl(){ }
@Override
public BusinessServiceImpl getInstance() {
if (instance == null) {
BusinessServiceImpl.instance = new BusinessServiceImpl();
}
return BusinessServiceImpl.instance;
}
}
How can I get access to getInstance from my main-class? I tried something like
private BusinessService service = new BusinessServiceImpl();
(does not work - Constructor from BusinessServiceImpl() is private)
But what else? Can you help me out?
Edit: I'm using Java Spring! I don't want to use Java Springs - Dependency Injection!
Greets, Mira
Every class can implement an interface, and a Singleton is just a "normal" class that makes sure that only one instance of it exists at any point in time apart from the other business logic it may implement.
Eager initialization: In eager initialization, the instance of Singleton Class is created at the time of class loading, this is the easiest method to create a Singleton class. By making the constructor as private you are not allowing other class to create a new instance of the class you want to create the Singleton.
Singleton is not a pattern to wrap globals. Singleton pattern should only be used to guarantee that one and only one instance of a given class exists during run time. People think Singleton is evil because they are using it for globals. It is because of this confusion that Singleton is looked down upon.
One of the main disadvantages of singletons is that they make unit testing very hard. They introduce global state to the application. The problem is that you cannot completely isolate classes dependent on singletons. When you are trying to test such a class, you inevitably test the Singleton as well.
The code you have provided is a bit mixed - up.
The whole point of controlling the instantiation of your singleton class means that you have to hide your constructor, so clients can't access it and create new instance at will.
Your factory method (a method, that creates the instance of your class) getInstance()
is an instance method. You have to create a new instance of BusinessService
in order to create an instance of BusinessService
- it's pretty obvious that won't work properly.
You have a few options to deal with your situation:
Remove the getInstance()
method from your interface and expose it in your implementation class as a static method. You should also limit the visibility of the constructor to private
.
public class BusinessServiceImpl implements BusinessService {
public static BusinessServiceImpl getInstance() {
// instantiation logic
}
private BusinessServiceImpl() {
// constructor logic
}
}
Use a factory class to instantiate your service and control the number of instances. This includes removing the getInstance()
method all together and limiting the visibility of your BusinessServiceImpl
class to package
.
class BusinessServiceImpl implements BusinessService {
BusinessServiceImpl() {
// constructor logic
}
}
// should be in the same package as BusinessServiceImpl
public final class BusinessServiceFactory {
private BusinessServiceImpl instance;
public BusinessService getInstance() {
// instance creation logic, same as singleton creation logic
}
}
Use an enum to deal with the singleton logic. Remove the getInstance()
method from your interface.
public enum BusinessServiceImpl implements BusinessService {
INSTANCE;
// implement the methods from your interface
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With