Java Singleton Mode

Source: Internet
Author: User

The following is an implementation of the singleton mode.
Public class station {

Private Static station ST = new station ();
Private int num = 10;
Private station (){

}

Public static station getinstance (){
Return st;
}

Public void upload (){
If (St. Num> 0 ){
Num --;
System. Out. println ("with tickets ");
} Else {
System. Out. println ("tickets sold out ");
}
}
}

Public class demo {

 

Public static void main (string [] ARGs ){

Station ST = station. getinstance ();
For (INT I = 20; I> 0; I --){
System. Out. Print (I + "\ t ");
Station. getinstance (). instances ();
}

}
}

The Singleton mode can be understood as an implementation of global variables in object-oriented languages. Of course, its role is not limited to this. We can analyze the ideas in the implementation process of the singleton mode to better understand what the design mode is, of course, it can also prove the importance of basic knowledge (the Foundation is King ).

First, let's analyze the meaning of a singleton. in Java, there can be only one instantiated object for a class. This is clearly not what we usually write about a common class. So how can we implement this requirement. Instantiation is a problem during class initialization, and Initialization is done by the constructor (of course, JVM also helps us do a lot ). Constructor. We need to set it to private. In this way, the class cannot be arbitrarily initialized. Where should we initialize it, and how can we call it.

A class that cannot be initialized, that is, the class that cannot be instantiated, we want to call it. This undoubtedly reminds us of the static keyword. A public static method can implement this requirement-that is, the method can be called without instantiating the class. So we wrote a public static method to obtain the unique instance of this class. This instance is an internal instance of the class and is also defined as a static variable (static is used all the time only for use without instantiation. Because static can be generated during class initialization, students who do not understand it can check my previous blog ...). Here, the implementation framework of a singleton mode has been set up.

In the example in this article, the lift () method is used to solve a specific problem. I have extended it under the single-sample framework. The background of this example is that there is a ticket point. We need to know whether we can buy a ticket when we buy a ticket. Obviously, this is a problem that can be solved in the singleton mode. Because there is only one variable to record the number of votes in the world, we cannot create multiple instances, so we lose the original meaning. So here we add the variable num for the number of votes, and if there are only 10 votes left.

Then, we set up a ticket sales method. If a ticket is sold, the number of tickets with a ticket is reduced by one. Otherwise, the ticket is sold out. (Simple judgment Statement)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.