Visualizing Thread-Safe Singletons in Java

Working in Java, we seldom have to create our handwritten singletons, for the existing frameworks like Spring creates these objects for us and maintains them. In enterprise applications, we get into framework disposal and let them create the singletons.

But it is interesting to know the singleton pattern and visualize the famous pattern in a multi-threaded environment. In this article, I will dive deep into this pattern with a multi-threaded environment view.

Migrating to MRTK2: Missing Singleton and 3DTextPrefab

If you are migrating from the HoloToolkit to Mixed Reality Toolkit 2 'cold turkey,' as I am doing for my AMS HoloATC app, a lot of things break, as I already said in the first post of this series. For things that you can tap, you can simply change the implementing interface from IInputClickHandler or IManipulationHandler to a couple of other interface and change the signature a bit — that's not complex, only tedious, depending on how much you have used it.

What I found really hard was the removal of the Singleton class and the 3DTextFab. I used both quite extensively. The first one I needed for like data access classes as the concept of services that was introduced in the Mixed Reality Toolkit 2 was not yet available, and the other... well basically all my texts were 3DTextPrefabs so any kind of user feedback in text format was gone. Because so much breaks at the same time, it's very hard to step by step rebuilding your app to a working condition. Basically you have to change everything before something starts to work again. Since I was still learning by doing, there was no way to test if I was doing things more or less right. I got stuck and took a radical approach.

Lazy Initialization Singleton Idioms

Frequently, I face requirements where I would like to use a singleton bean instead of creating new objects — and this happens all of the time. Beans that have no relevant state or are expensive to create lend itself to become a singleton. The conventional singleton idioms are fairly easy and go something like this:

// private constructor singleton idiom
public class PrivateConstructorSingleton {
   public static final PrivateConstructorSingleton INSTANCE = new PrivateConstructorSingleton();
   private PrivateConstructorSingleton() {}
}
// lazy loading singleton idiom
public static class LazyLoadingSingleton {
   private LazyLoadingSingleton() {}
   private static class LazyHolder {
       static final LazyLoadingSingleton INSTANCE = new LazyLoadingSingleton();
   }
   public static LazyLoadingSingleton getInstance() {
       return LazyHolder.INSTANCE;
  }
}


Creational Design Pattern Series: Singleton Pattern

Every now and then, it’s important to have only one instance for a class. Usually, singletons are used for centralized management of resources and they provide a global point of access to themselves.

Some of the common examples where singleton pattern is used include logger classes, configuration classes, etc.

Design Patterns in Java: Singleton

A design pattern is defined as the re-usable form of a solution to a design problem. With a design pattern, you can document a solution to a design problem. Design patterns were introduced by the architect Christopher Alexander and have been adapted for various other disciplines and are very popular among developers, as they provide solutions to general problems that software developers face. It provides a template to solve common problems while designing a system or an application.

Design patterns act as a common language between developers so that each developer easily understands the concept while working with common problems.