技术控

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

[其他] Java Singleton Design Pattern

[复制链接]
fpwx0sOl 发表于 2016-10-5 00:29:48
104 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-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

  [code]package com;

public class SingletonDesignPattern {
//instance will be created at the time of class loading
private static SingletonDesignPatternobj = new SingletonDesignPattern();

//private constructor
private SingletonDesignPattern(){}

//global point of access to the instance
public static SingletonDesignPatterngetInstance(){
return obj;
}

public void printMessage(){
System.out.println("Java Singleton Design Pattern Using Early Instantiation");
}
}
[/code]  Demo.java

  [code]package com;

public class Demo {
public static void main(String...s){
SingletonDesignPatternobj = SingletonDesignPattern.getInstance();
obj.printMessage();
}
}
[/code]  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

  [code]package com;

public class SingletonDesignPattern {
private static SingletonDesignPatternobj = null;

//private constructor
private SingletonDesignPattern(){}

//global point of access to the instance
public static SingletonDesignPatterngetInstance(){

//instance will be created when requested
if(obj == null){
obj = new SingletonDesignPattern();
}

return obj;
}

public void printMessage(){
System.out.println("Java Singleton Design Pattern Using Lazy Instantiation");
}
}
[/code]  Demo.java

  [code]package com;

public class Demo {
public static void main(String...s){
SingletonDesignPatternobj = SingletonDesignPattern.getInstance();
obj.printMessage();
}
}
[/code]  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.
  [code]//global point of access to the instance
public static SingletonDesignPatterngetInstance(){
//instance will be created when requested
if(obj == null){
synchronized(SingletonDesignPattern.class){
if(obj == null){
obj = new SingletonDesignPattern();
}
}
}

return obj;
}
[/code]  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
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

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

返回顶部 返回列表