Single-Case mode

Source: Internet
Author: User

Concept:
  In Java, the singleton pattern is a common design pattern, and the singleton pattern is divided into three kinds: lazy type single case, a hungry man type single case, registration type single case three kinds.
There are a few features of the Singleton model:
  1, the Singleton class can have only one instance.
  2. The Singleton class must create its own unique instance itself.
  3. The Singleton class must provide this instance to all other objects.
Singleton mode ensures that a class has only one instance, and instantiates itself and provides this instance to the entire system. In a computer system, the driver objects of the thread pool, cache, log Object, dialog box, printer, and video card are often designed as singleton. These apps have more or less the functionality of the resource manager. Each computer can have several printers, but only one printer Spooler to prevent both print jobs from being output to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to prevent a communication port from being called simultaneously by two requests. In short, the selection of Singleton mode is to avoid inconsistent state, to avoid long-running political.

First, we look at a classic single-instance implementation.

Public class Singleton {

Private Static Singleton uniqueinstance = null;

Private Singleton () {

Exists only to defeat instantiation.

}

Public Static Singleton getinstance () {

if (uniqueinstance = = null) {

uniqueinstance = new Singleton ();

}

return uniqueinstance;

}

Other methods ...

}

Singleton by restricting the construction method to private to prevent the class from being instantiated externally, the unique instance of Singleton can only be accessed through the getinstance () method within the same virtual machine scope. (In fact , the Java reflection mechanism is the ability to instantiate a class constructed as private, which basically invalidates all Java Singleton implementations.) This issue is not discussed here, and it is deceiving to assume that the reflection mechanism does not exist. )

However, the above implementations do not take into account thread safety issues. Thread safety refers to the idea that if your code is in a process where multiple threads are running concurrently, these threads may run the code at the same time. If the result of each run is the same as the single-threaded run, and the value of the other variable is the same as expected, it is thread-safe. Or, the interface provided by a class or program for a thread is an atomic operation or a switchover between multiple threads does not result in ambiguity in the execution of the interface, that is, we do not have to consider the problem of synchronization. Obviously the above implementations do not meet the requirements of thread safety, and many singleton instances are likely to occur in concurrent environments.

 Public classTeststream {PrivateString name;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     //there can be only one instance of the class    PrivateTeststream () {}//Private non-parametric construction method//the class must create its own//There are 2 different ways    /*private static final Teststream ts=new Teststream ();*/    Private StaticTeststream ts1=NULL; //This class must automatically provide this instance object to the entire system     Public StaticTeststream gettest () {if(ts1==NULL) {Ts1=NewTeststream (); }        returnTs1; }     Public voidGetInfo () {System.out.println ("Output message" +name); }}
 Public classTestmain { Public Static voidmain (String [] args) {Teststream s=teststream.gettest (); S.setname ("Zhang Xiaoxiang");        System.out.println (S.getname ()); Teststream S1=teststream.gettest (); S1.setname ("Zhang Xiaoxiang");        System.out.println (S1.getname ());        S.getinfo ();        S1.getinfo (); if(s==S1) {System.out.println ("Create the same instance"); }Else if(s!=S1) {System.out.println ("Not created the same instance"); }Else{System.out.println ("Application Error"); }    }}

Single-Case mode

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.