SingleTon () {}//class cannot access public static SingleTon getinstance () {return uniqueinstance;} Other useful method of a single-piece class, a single-piece class can also be a generic class, with general data and methods}Analysis: Initializing a static variable, the JVM creates a unique singleton object as soon as the class loads;you don't have to worry ab
() { if (single = null) {Single = new Singleton (); } return single; } }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.
class object.
The Singleton mode can be divided into stateful and stateless. Stateful Singleton objects are also variable Singleton objects. When multiple single-State objects are together, they can serve as a State Warehouse. The stateless singleton object, that is, the unchanged
the write connection pool uses the first and third methods. Use the first method to make sure that the connection pool exists. Use the third method to make sure that the connection pool exists.The first method is as follows:Declare the constructor as private to avoid creating an instance in any way other than the methods we provide. If you do not declare the constructor as private, the compiler automatically synchronizes a default friendly constructor. The following is an
Java Singleton ModeConcept: java Singleton mode is a common design mode. The Singleton mode can be written in several ways. Here we mainly introduce three types: the lazy singleton and ELE. Me
Single-Case modeSingleton mode (Singleton pattern) is one of the simplest design patterns in Java. This type of design pattern belongs to the Create pattern, which provides an optimal way to create an object.This pattern involves a single class that is responsible for creating its own objects, while ensuring that only a single object is created. This class provides a way to access its only object, which can
Single-case design mode-------lazy, a hungry manThe singleton design pattern is a very common design pattern. Two types of singleton design patterns are introduced here. Lazy type and a hungry man typeFirst, talk about the characteristics of a single-case design pattern:>>1. The singleton design pattern guarantees that there is only one instance of a class. >>2.
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 Singl
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 Singl
chosen according to your business needs.
A Hungry man
Standard A Hungry man (Safety protection aspect enumeration single example better than standard a hungry man)Thread-safe, efficient, and not lazy to load
Enumeration single ExampleThread-safe, efficient, lazy loading (natural avoidance of reflection and deserialization)?
Lazy (Static inner class of efficiency is better than standard idler)
Standard
Singleton in Design Mode)
Banqiao peopleHttp://www.jdon.com
Definition:
The Singleton mode ensures that only one instance of a class exists in a Java application.
How to use it?
The first form:
Public class Singleton {
Second form:
Public class Singleton {
Key to im
In the interview often asked the Java singleton model, the problem can be very good to examine the candidate's understanding of knowledge points. Singleton mode requires that only one instance object exists when the system is running.Let's take a detailed look at the key points of knowledge, and introduce five implementations, along with their pros and cons. The
In today's teacher's lecture 1 learned the role of single-row modelThe Java singleton pattern ensures that a class has only one instance, provides this instance itself and provides this instance to the system as a whole.Characteristics:1, a class can have only one instance;2, create this instance yourself;3, this instance is used throughout the system.--------------------------------The main purpose of the
man
Standard A Hungry man (Safety protection aspect enumeration single example better than standard a hungry man)Thread-safe, efficient and lazy to load
Enumeration single Examplethread safe, efficient and lazy to load (natural avoidance of reflection and deserialization)?
Lazy (Static inner class of efficiency is better than standard idler)
Standard lazythread safe, inefficient, lazy to load
Double detec
() {4 } 5 } 6This approach is advocated by effective Java author Josh Bloch, which not only avoids multi-threaded synchronization problems, but also prevents deserialization from recreating new objects, which can be a very strong barrier, but Personally think that because of the 1.5 to join the enum characteristics, in this way to write inevitably people feel unfamiliar, in the actual work, I also rarely see someone write so. Seventh type (doub
-threading Both of these singleton modes are simple to implement, and there is no problem with single-threaded, but it is possible to have problems in the case of multithreading. For example, the following scenario: There is a singleton class s, thread A calls S of getinstance () to create an instance, to determine the end of instance is null but at this time be
Concept: Java The single case pattern is a common design pattern, there are several types of single-case pattern, here are three kinds: lazy type single case, a hungry man type single caseThe singleton mode has the following characteristics: 1 , the Singleton class can only have one instance. 2 , The Singleton class
Singleton mode: Ensures that one object has and only one during the whole project operation.Mainly include: 1, a hungry man type; 2, lazy type.1 classSingleton1//a hungry man type2 {3 PrivateSingleton1 () {}//privatization of the construction method, not allowing the outside world to create objects4 Private Static FinalSingleton1 instance=NewSingleton1 ();5 /*A hungry man is an assignment when it's declared.6 The disadvantage is that when
Singleton ensures that a class has only one instance and provides a global access point.
Those who have played counter-terrorism elites should know that there is a console in the game. We can call this console by pressing the 'key (wavy line key. The purpose of the console is to facilitate debugging by developers. Of course, we can also modify some game parameters in it. For example, input SV_GRAVITY 100 to
Singleton mode is one of 23 design patterns, about its introduction, has been countless, the general online implementation of the way is nothing but what lazy type, a hungry man type, internal class, enumeration, volatile plus double check lock and so on, today I think of a new way of realization, Using the atomic class provided by J.U.C, the code is as follows:class SingleTon { privatestaticnew AtomicRe
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.