Java Singleton Pattern Singleton Mode
Wikipedia: When you click open link to apply this mode, the class of the singleton object must ensure that only one instance exists.
Key points:
1. private constructor-other classes cannot instantiate a new object.2. Private reference-External modification is not allowed.3. The pub
Java Singleton mode and java mode
I. What is Singleton mode?
Singleton: ensure that a class has only one instance, and provide a global access point to access it.
The Singleton mode is one of the common software design patterns.
gets the object, it has to do the synchronous synchronized operation, the cost is large, and the synchronization operation is only valid at the first new time (then each call does not do the new operation, the equivalent of read-only not write), so it can be the new object when the class is loaded, and then does not do the new operation. Improvements are as follows:publicclass UrgSingleton { privatestaticnew UrgSingleton(); privateUrgSingleton() {} publicstaticgetInstance() { re
prevent deserialization from causing new objects to be recreated. But it is rare to see someone write like this, perhaps because they are not familiar with it. SummaryA summary is certainly essential, and the above is just a list of our common singleton implementations. Of course not completely, for example, we can also use static code block way to implement the lazy code, but here is not one by one exampl
What is a single case, and what is its role?
The singleton pattern is understood from the name. Single is once. The example is to instantiate an object, that is, I don't care how many times I use it to instantiate an object. OK, what is the function of this design pattern?
The singleton mode can ensure that there is only one instance of a class in the system,
Singleton () {}public static Singleton getinstance () {return Singletonholder.instance;} private static class Singletonholder{private static final Singleton instance = new Singleton ();}}When the Singleon class is loaded for the first time, the instance is not initialized, which causes instance to be initialized only
Directly on the code:1 Public enumBoss {2 3 INSTANCE;4 5 PrivateString name;6 7 Public voiddosomething () {8SYSTEM.OUT.PRINTLN (name + "is doing something now ...");9 }Ten One PublicString GetName () { A returnname; - } - the Public voidsetName (String name) { - This. Name =name; - } - +}Note: The enum in Java is a thoroughly-bottomed class that can be freely added to other properties and methods, so
. Not the same, it is inside the inner class to create an object instance. In this case, as long as the application does not use an inner class, the JVM does not load the singleton class, nor does it create a singleton object, thus allowing lazy lazy loading. In other words, this approach can guarantee both lazy loading and thread safety.5. EnumerationLet's look at the last way to implement this article: en
In the book "Java and Patterns" of Dr. Shanhong, this describes the singleton pattern: As an object's creation mode, Singleton mode ensures that a class has only one instance, and instantiates itself and provides this instance to the system as a whole. This class is called a singleton class.
Structure of the
, nor does it create a singleton object, thus allowing lazy lazy loading. In other words, this approach can guarantee both lazy loading and thread safety.5. EnumerationPublic enum singleton{ instance; public void Whatevermethod () {} }There are common drawbacks to the four ways in which you can implement a single example:1) Additional work is required fo
single State definition :The main purpose of Singleton mode is to ensure that only one instance of a class classes exists in a Java application.
In many operations, such as the creation of a directory database connection requires such a single-threaded operation.
Also, the singleton can be state-organized; In this way, multiple single state classes can be service
Examples of php Singleton mode and php mode are shared. Examples of the php Singleton mode are shared, while examples of the php mode are shared. The Singleton mode is mainly used for database connections to ensure that there is only one instance for a database class, this example of the real php
Http://www.runoob.com/design-pattern/singleton-pattern.htmlThe singleton mode may be the least code mode, but less does not necessarily mean simple, want to use good, with the singleton mode, it really cost a brain. This paper makes a summary of the common pattern in Java, if there are errors or omissions, please corre
Java design mode: Singleton Design Mode
1. What is the design model:
The concept of the design model first comes from other industries: when building a house in the construction industry in the early stages, there must be a lack of experience and disorder. This will cause many problems in the development process of the industry, through continuous experience accumulation, our predecessors have put forward r
[Java design mode] java Singleton mode and java Design Mode
What is the singleton mode:The Singleton mode ensures that a class has only one instance, and the instance is self-instantiated and provided to the entire system. In comp
The first type (lazy, thread insecure):Java code
public class Singleton {
private static Singleton instance;
Private Singleton () {}
public static Singleton getinstance () {
if (instance = = null) {
Instance = new
Java design mode: Singleton mode; java Design Mode
In the 23 GoF design modes, the singleton mode is relatively simple. However, sometimes the simpler the things, the more likely the problems will occur. The following is a detailed discussion of the singleton design mode.
In
As the object creation mode, the singleton mode ensures that a class has only one instance, and the instance is self-instantiated and provided to the entire system. This class is called a singleton class.
Structure of Singleton ModeFeatures of Singleton mode:A singleton clas
instance, which differs from the third and fourth ways (very subtle differences): The third and fourth ways are as long as the Singleton class is loaded, Then instance will be instantiated (without the lazy loading effect), and this is singleton class is loaded, instance not necessarily initialized. Because the Singletonholder class is not actively used, the load Singletonholder class is displayed only if
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 (double check lock):1 public class
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.