A brief introduction to Java single example schema _java

Source: Internet
Author: User

The example in this article describes the Java single case pattern, which is a very important concept in Java programming. Share to everyone for your reference. The specific analysis is as follows:

The so-called list mode is the entire application process to provide only a single instance of the outside world, that is, the application of only one instance, so you do not have to repeatedly create instances . Then, according to his requirements, look at one of the simplest examples of the code in the single example pattern:

public class Singleton {
  private static Singleton single = new Singleton ();
  
  Private Singleton () {
    
  } public
  
  static Singleton getsingletoninstance ()}


Through this code, we find that to implement this instance of the Singleton class, we have to pass the constructor, but his constructor is private, so it is impossible to implement his instantiation in other classes, but you can use the Getsingletoninstance method, Can return an instance single because he is a static function of public and can be called by another class. This is a simple single case pattern. Of course, you can also put a single statement in the Getsingletoninstance method to achieve.

Summarize the characteristics of the single example pattern:

1. The construction method is private decorated .

2. There is a private instance of static application .

3. there is a static public method that returns an instance of the class .

In fact, these three characteristics are entirely determined by the requirement of a single case model.

I hope this article will help you with your study of Java programming.

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.