• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 552
  • Last Modified:

Explain Singleton Pattern Could you please give me the Pseudocode for singleton pattern

Explain Singleton Pattern Could you please give me the Pseudocode for singleton pattern
0
Manikandan Thiagarajan
Asked:
Manikandan Thiagarajan
  • 2
1 Solution
 
reijnemansCommented:
0
 
reijnemansCommented:
Even better is to use the singleton this way, now it is thread save:

public class Singleton {
        // Private constructor prevents instantiation from other classes
        private Singleton() { }
 
        /**
        * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
        * or the first access to SingletonHolder.INSTANCE, not before.
        */
        private static class SingletonHolder { 
                public static final Singleton instance = new Singleton();
        }
 
        public static Singleton getInstance() {
                return SingletonHolder.instance;
        }
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now