-1

I was just having this small conversation with a buddy and he's insisting that the singleton used for Dagger is better than making one through old-school-static-private-variables way. Not that I'm sure he's wrong, I just want to clear up why is it better.

What is the difference between these two implementation methods (DI vs normal static)?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Julious Igmen
  • 268
  • 3
  • 9

1 Answers1

0

This Java best practices book explains why you should enforce the Singleton property with a private constructor or an Enum type. The chapter is quite long, so keeping it summarized:

Making a class a Singleton can make it difficult to test its clients, as it’s impossible to substitute a mock implementation for a singleton unless it implements an interface that serves as its type. Before release 1.5, there were two ways to implement singletons. Both are based on keeping the constructor private and exporting a public static member to provide access to the sole instance. In one approach, the member is a final field:

// Singleton with public final field
public class Elvis {
public static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public void leaveTheBuilding() { ... }
}

In the second approach to implementing singletons, the public member is a static factory method:

// Singleton with static factory
public class Elvis {
private static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public static Elvis getInstance() { return INSTANCE; }
public void leaveTheBuilding() { ... }
}

There is a third approach to implementing Singletons. By simply make an enum type with one element:

// Enum singleton - the preferred approach
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}

This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks.

While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

ekostadinov
  • 6,880
  • 3
  • 29
  • 47