Java Singleton Pattern Singleton Mode
Wikipedia: When you click open link to apply this mode, the class of the singleton object must ensure that only one instance exists.
Key points:
1. private constructor-other classes cannot instantiate a new object.
2. Private reference-External modification is not allowed.
3. The public static method is the only way to obtain objects.
Public class SingleObject {private static SingleObject instance = new SingleObject (); // create an object of SingleObjectprivate SingleObject () {}// private constructor, therefore, the public static SingleObject getInstance () {// provides a static method to get its static instance to outside world. return instance;} public void showMessage () {System. out. println ("Hello world... ");}}
Public class SingletonPatternDemo {public static void main (String [] args) {// SingleObject object = new SingleObject () // invalid constructor SingleObject () is not visibleSingleObject object = SingleObject. getInstance (); // Get the only object availableobject. showMessage ();}}
Output result: Hello world...
More learning, especially,Singleton mode in multi-threaded Environments, Click:
Wikipedia-http://zh.wikipedia.org/wiki/%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F
Story telling, learning (Java) design mode-singleton mode click to open the link
Http://zz563143188.iteye.com/blog/1847029
References:
Http://www.tutorialspoint.com/design_pattern/design_pattern_quick_guide.htm