Android Singleton Implementation
Post
Cancel

Singleton Implementation

A - Intent

  • Ensure only ONE instance of the class is created
  • Provide global access to that instance

B - Usage

Singleton will be applied when you want to have exactly ONE instance of a class, no less, no more. And that object can be access anywhere in your code.

Some cases that Singleton is usually used:

  • Logger class
  • Configuration class
  • Access resources in shared mode
  • Factory design pattern that implemented as Singleton

C - Singleton Structure

java-singleton-design-pattern

Singleton is the simplest design pattern (that’s why I wrote an entry about it LOL).

As you can see in the firgure above, a Singleton class consists of:

  • a private instance that contains the Singleton object
  • a private contructor that prevent creating more than one instance
  • a public static method getInstance() to provide global access to the Singleton object

That’s all. Only one class :)))

D - Singleton Implementation

1. Lazy initialization

This way is a classical Singleton implementation.

public static getInstance(){
	if(instance == null){
		instance = new Singleton();
	}
	return instance
}

Pros:

  • The instance will be created only when client application need to use it - lazy initialization

Cons:

  • When there're more than 1 thread try to access the Singleton object at the same time. It might create more than 1 instance! So this implementation is not thread safe!

To prevent multi-thread concurrency, let’s take a look at the second way!

2. Eager initialization

The easiest way to prevent multi-thread concurrency is eager initialization.

private Singleton instance = new Singleton();

public static getInstance(){
	return instance
}

Pros:

  • Easy to implement
  • Prevent multi-thread concurrency

Cons:

  • The Singleton instance was created even the client app might not be using it
  • Client app cannot pass any arguments to the constructor

3. Thread safe with lazy initialization

If you want to resolve all the disadvantages of eager initialization, try this way:

public static getInstance(){
	if (instance == null){
		synchronized(Singleton.class) {
			if (instance == null) {
				instance = new Singleton();
			}
		}
	}

	return instance
}

With the synchronized keyword, you can handle multi-thread concurrency case but it will be very costly. That’s why I use the double check locking mechanism( cehck in an unsynchronized block if the object is null and if not to check again and create it in an syncronized block). If we see that the singleton object is already created we just have to return it without using any syncronized block.

Pros:

  • Thread safe
  • Lazy initialization
  • Client app can pass arguments to constructor
  • Reduce costly of synchronized block code

Cons:

  • Extra if condition

4. Enum Singleton

You also can using Enum to implement Singleton:

public enum Singleton {
	Singleton instance;
	public void doStuff(){
		return instance;
	}

}

Pros:

  • Prevent multi-thread concurrency
  • Easy to implement

Cons:

  • Cannot lazy initialization

E - Singleton vs Static Class

  • Singleton can implement interfaces and extends classes but Static Class cannot
  • Static Class can only have static members, methods
  • Static Class cannot load lazily like Singleton
  • Singleton is stored in Heap and Static Class is stored in Stack

Anything else? Just comment below! :)

 

This post is licensed under CC BY 4.0 by the author.