FinalSingleton8 INSTANCE =NewSingleton8 () {}; }Private Singleton8(){}; Public StaticSingleton8getinstance(){returnSingletonholder.instance; } PublicObjectReadresolve() {returnSingletonholder.instance; }}Test results:不能用Class.newInstance创建,因此Class.newInstance校验通过不能用反射方式创建,因此ContructorInstance校验通过Serializable校验通过This is primarily the method Readresolve, whose return result is used instead of the deserialized resultSingle Case 9Enumeration Singleton,re
Comparison between static methods and Singleton mode in Java, and java static example Mode
Differences
Singleton Mode
Static Method
Instance
Create an instance
None
Run
Class instance method
Class MethodYou can als
In the daily work, there are many objects, we only need one. For example: thread pool, caching, registry, and so on. If you create multiple instances, you can cause many problems, such as abnormal program behavior, excessive use of resources, and so on. This requires controlling the construction of the object so that it can produce only one object. This is the design pattern--singleton (single
This example introduces the two most common examples of single example design patterns--a hungry man single example and lazy single example.
1. Class Diagram
2. Java Implementation CodePackage Cn.edu.ynu.sei.singleton;
/**
* "A Hungry Man" type of single case class
Singleton mode: used to ensure that a class has only one instance during a certain degree of Operation
Advantage: to save resources as much as possible
Application scenarios
--------------------
The Singleton mode is implemented by modifying the access permissions of the constructor.
For example
Package com. lixiyu; public class TestExample1 {public static void m
In the daily work, there are many objects, we only need one. For example: thread pool, cache, registry, etc. If multiple instances are produced, many problems can result, such as abnormal program behavior, excessive resource usage, and so on. This requires controlling the construction of an object so that it can produce only one object. This is the design pattern to be talked about-Singleton (
1. Definition of a singleton patternA singleton pattern (Singleton pattern) is a simpler pattern.The original definition is as follows: Ensure a class has only one instance, and provide a global point of access to it. That is, ensure that there is only one instance, and instantiate it yourself and provide this instance to the system as a whole. A generic class fo
A singleton object (Singleton) is a common design pattern. In Java applications, singleton objects guarantee that only one instance of the object exists in a JVM. There are several benefits to this model:1, some classes are created more frequently, for some large objects, this is a lot of overhead.2, eliminate the new
object just new public StaticSingleton getinstance () {returninstance; } public voidTest () {System.out.println ("singleton"); } } /********************************************************************/ 2. Thread safety problems in singleton modeThe above is a classic Single-mode program, and this program does not produce thread synchronization problems, because the class was first loaded
A singleton object (Singleton) is a common design pattern. In Java applications, singleton objects guarantee that only one instance of the object exists in a JVM. There are several benefits to this model:1, some classes are created more frequently, for some large objects, this is a lot of overhead. 2,eliminate the new
!"); } Console.ReadKey();}3. Example running resultFour Example Analysis (Example) 1, sceneIn the mail sending mechanism, you need to log the messages that have been sent. Only one process is allowed to operate on the TXT document at the same time, and a singleton mode is more appropriate. Structure as shownWrite
The English original of the singleton mode is: Ensure a class has only one instance, and provide a global point of access to it. This means ensuring that a class has an instance and provides this instance to the entire system. The singleton mode is primarily to ensure that only one instance exists. There are two forms of representation in the Java language:A hung
GOF23 in Java (23 design mode)---------Singleton mode (Singleton) In the Java this language, its advantage lies in its own portability above, and to be portable, it needs an intermediary as a translation work, in order to achieve local and Java unity, but in this case it is
Singleton mode is the simplest and most understandable pattern in the development model. To put it simply, it means to always keep an instance of the meaning. However, the Java class can wear a number of instances, then, how to implement it?As the name implies, Singleton mode is just one instance. The singleton pattern
is often present in the DAO layer (for example, sessionfactory), if repeated initialization and release, it will occupy a lot of resources, and use singleton mode to the memory can be more resource-saving.5. Static methods have higher access efficiency.6. The singleton mode is easy to test.A few misconceptions about static classes:Myth One: Static methods reside
Singleton mode is very simple, that is, in the constructor and add a constructor, access permissions are private, this pattern is simple, but the risk of simple, risk? What risk? In a B/s project, each HttpRequest request to the Java EE container creates a thread, and each thread creates the same singleton object./*** I ' m glad to share my knowledge.* China's
This article mainly introduces how to implement singleton () singleton mode in php. It briefly describes the implementation process of singleton () singleton mode in the form of instances. For more information, see
This article mainly introduces how to implement singleton (
Java Singleton mode and java Singleton Mode
The Singleton mode is written in many books as follows:
public class SingleTon1 {private SingleTon1(){}private static SingleTon1 instance = null;public static SingleTon1 getInstance(){if(instance == null){instance = new SingleTon1(
For an example of a singleton pattern in PHP design mode, the principles and Techniques of PHP singleton mode are used to learn the implementation of PHP singleton mode.
Singleton mode (Duty mode): An object (before learning the design pattern, the need to understa
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.