April 2017
Intermediate to advanced
564 pages
24h 7m
English
There are some commonly used variations of the singleton pattern that are applicable based on the given situation. Out of these, the most common ones are lazy initialization and the double check locking pattern.
Lazy initialization singleton basically creates its one and only instance upon its first call to get an instance instead of pre-creating it. Refer to the following simple code sample:
public sealed class LazySingleton { private static LazySingleton instance = null; private LazySingleton() { } public static LazySingleton getInstance() { if (instance == null) instance = new LazySingleton(); return instance; } }
The double check locking pattern is basically a thread-safe singleton pattern ...
Read now
Unlock full access