Home > Java > javaTutorial > body text

Detailed explanation of java singleton pattern

巴扎黑
Release: 2017-09-18 11:32:40
Original
1179 people have browsed it

This article mainly introduces the relevant information of java singleton mode (hungry man mode and lazy man mode). I hope that everyone can master this part of the content through this article. Friends in need can refer to it

java singleton pattern

Hungry-style singleton

For the Hungry-Han pattern, we can understand it this way: The singleton class is very hungry. It desperately needs to eat, so it creates the object as soon as the class is loaded.

Lazy Singleton Class

Regarding the lazy mode, we can understand it this way: the singleton class is very lazy and will only do it when it needs it. Action, never know to prepare early. It only determines whether there is an object when it needs an object. If not, it immediately creates an object and then returns. If there is an object, it does not create it and returns immediately.

The singleton design pattern is often used in JDBC linked databases

Note:

1 We commonly use the first hungry Chinese style, Because:

(1) Since the singleton design pattern is adopted, it is to use the object of the singleton class, so we instantiate it first.

( 2) There are certain security risks in the lazy style, and the synchronization keyword synchronized needs to be added, otherwise it will not be a singleton, but the efficiency will be slightly lower after synchronized is added

2 In the first type In the method, the code private static final Single SINGLE=new Single(); why is there final?

Because classes modified by the final keyword cannot be inherited, and member variables modified by final cannot Modified.

Final is used here to strengthen and highlight the concept of "the same object" - there is only one object, and it cannot be modified.

If you do not modify Single SINGLE with final, then there will be a situation: the business is very complex, and the object is modified inadvertently, causing various errors.

The first ( Hungry Chinese style):

Ideas:

(1) Do not allow other classes to create objects of this class. That is, set the constructor to private!
(2) Customize an object of this class
(3) Provide the customized object. That is, define a get method, and the return value is an object of this class.

Analysis:

The first step: privatize the constructor in the singleton class and use a get() function to provide such objects to the outside, so Other classes cannot construct objects of this class.
Step 2: But if other classes want to call the method in this singleton class, they can only use the internal name.Method name () to implement it, which requires that this method must be static.
Step 3: Because static methods can only access static members! So set SINGLE to static


public class SingleDemo { 
  public static void main(String[] args) { 
     Single s1=Single.getSingle(); 
     s1.setNumber(44); 
     Single s2=Single.getSingle(); 
     s2.setNumber(55); 
     System.out.println("s1.number="+s1.number); 
     System.out.println("s2.number="+s2.number); 
     if(s1.equals(s2)){ 
        System.out.println("s1与s2是同一对象即:s1==s2");//if条件成立 
       } 
   } 
} 
 
 
class Single{ 
  int number; 
  public int getNumber() { 
       return number; 
     } 
  public void setNumber(int number) { 
       this.number = number; 
     } 
  private Single(){};//第一步 
  private static final Single SINGLE=new Single();//第三步 
  public static Single getSingle(){//第一步和第二步 
       return SINGLE; 
  } 
}
Copy after login

The second type (lazy style):


package cn.com; 
public class SingleDemo2 { 
   public static void main(String[] args) { 
      Single s1=Single.getSingle(); 
      s1.setNumber(44); 
      Single s2=Single.getSingle(); 
      s2.setNumber(55); 
      System.out.println("s1.number="+s1.number); 
      System.out.println("s2.number="+s2.number); 
      if(s1.equals(s2)){ 
        System.out.println("s1与s2是同一对象即:s1==s2");//if条件成立 
       } 
     } 
 
 
} 
 
 
class Single{ 
   int number; 
   public int getNumber() { 
       return number; 
     } 
   public void setNumber(int number) { 
       this.number = number; 
     } 
   private Single(){}; 
   private static Single SINGLE=null; 
   public static synchronized Single getSingle(){//多线程时加上synchronized是关键!!! 
      if(SINGLE==null){ 
         SINGLE=new Single(); 
         return SINGLE; 
       } 
      else{ 
         return SINGLE; 
        } 
   } 
}
Copy after login

The above is a detailed explanation of the Java singleton design pattern. If you have any questions, please leave a message or go to the community of this site for discussion. Thank you for reading. I hope it can help everyone. Thank you for your support of this site!

The above is the detailed content of Detailed explanation of java singleton pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!