请选择 进入手机版 | 继续访问电脑版

技术控

    今日:41| 主题:54631
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Java Singleton Design Pattern

[复制链接]
fpwx0sOl 发表于 2016年10月5日 00:29
146 0

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
       
  • Defining a class that can have single instance and provides a global access point to get the instance.   
  • This design pattern is mainly used in multi-threaded and database applications.   
  • It saves memory because only one instance is created and used again and again.  
   

Java Singleton Design Pattern

Java Singleton Design Pattern-1-技术控-following,provides,database,loading,because

  Java Singleton Design Pattern

   There are various ways to implement singleton design pattern. Here I am discussing two popular ways, Early Instantiation and Lazy Instantiation .
  A singleton class must have following three things.
   Private Static Variable:It contains the instance of the class.
   Private Constructor:It prevents further instantiation of class.
   Public Static Method:It is a global access point to get the instance of class from outer world.
  Early Instantiation

  In this implementation, the instance is created at the time of class loading. Below example shows how to implement singleton design pattern using early instantiation.
  SingletonDesginPattern.java

  1. package com;
  2. public class SingletonDesignPattern {
  3. //instance will be created at the time of class loading
  4. private static SingletonDesignPatternobj = new SingletonDesignPattern();
  5. //private constructor
  6. private SingletonDesignPattern(){}
  7. //global point of access to the instance
  8. public static SingletonDesignPatterngetInstance(){
  9. return obj;
  10. }
  11. public void printMessage(){
  12. System.out.println("Java Singleton Design Pattern Using Early Instantiation");
  13. }
  14. }
复制代码
Demo.java

  1. package com;
  2. public class Demo {
  3. public static void main(String...s){
  4. SingletonDesignPatternobj = SingletonDesignPattern.getInstance();
  5. obj.printMessage();
  6. }
  7. }
复制代码
Output

  Java Singleton Design Pattern Using Early Instantiation
  Lazy Instantiation

  In this implementation, the instance is created at request time. Below example shows how to implement singleton design pattern using lazy instantiation.
  SingletonDesginPattern.java

  1. package com;
  2. public class SingletonDesignPattern {
  3. private static SingletonDesignPatternobj = null;
  4. //private constructor
  5. private SingletonDesignPattern(){}
  6. //global point of access to the instance
  7. public static SingletonDesignPatterngetInstance(){
  8. //instance will be created when requested
  9. if(obj == null){
  10. obj = new SingletonDesignPattern();
  11. }
  12. return obj;
  13. }
  14. public void printMessage(){
  15. System.out.println("Java Singleton Design Pattern Using Lazy Instantiation");
  16. }
  17. }
复制代码
Demo.java

  1. package com;
  2. public class Demo {
  3. public static void main(String...s){
  4. SingletonDesignPatternobj = SingletonDesignPattern.getInstance();
  5. obj.printMessage();
  6. }
  7. }
复制代码
Output

  Java Singleton Design Pattern Using Lazy Instantiation
  To make the class thread safe we can create the instance inside synchronized block. It can be done in following way.
  1. //global point of access to the instance
  2. public static SingletonDesignPatterngetInstance(){
  3. //instance will be created when requested
  4. if(obj == null){
  5. synchronized(SingletonDesignPattern.class){
  6. if(obj == null){
  7. obj = new SingletonDesignPattern();
  8. }
  9. }
  10. }
  11. return obj;
  12. }
复制代码
Comment below if you found any information incorrect or missing in above tutorial.



上一篇:Fixing an XSS vulnerability in marked
下一篇:It took 628 minutes to make this time tracking tool
*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )

© 2001-2017 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表