Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Singleton with Interfaces Java

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

like image 924
Mira Mira Avatar asked Aug 22 '16 08:08

Mira Mira


People also ask

Can a singleton have an interface?

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.

What is an efficient way to implement a singleton pattern in Java?

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.

When we should not use Singleton pattern?

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.

What is the disadvantage of Singleton pattern in Java?

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.


1 Answers

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:

  1. 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
        }
    }
    
  2. 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
        }
    
    }
    
  3. 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
    }
    
like image 172
Danail Alexiev Avatar answered Oct 22 '22 04:10

Danail Alexiev