JAVA design patterns-Singleton and singletonReprinted please indicate the source: http://blog.csdn.net/l1028386804/article/details/45441169I,Overview
Ensure that a class has only one instance and provides a global access point to it.II,
Applicability
1. When the class can only have one instance and the customer can access it from a well-known access point.
2. When this unique instance should be extensible t
existswe only need to lock the instance before it is created to ensure that only one thread creates the instance. Once the instance has been created, we don't need to lock the operation anymore. Therefore we improve for example the following:Public class Singleton3 {private static Singleton3 instance = null;Private Singleton3 () {}Public static Singleton3 getinstance () {if (instance = = null) {synchronzied (singleton3.class) {Singleton3 temp = insta
Design Mode-singleton mode (02) and design mode example mode 02Definition
Singleton Pattern is the simplest design Pattern.Ensure a class has only one instance, and provide a global point of access to it.Make sure that a class has only one instance and the instance is automatically instantiated and provided to the entire system.
The
instance; } 3.) Internal class-Type singleton classIn the inner class, deferred loading is implemented, only we call the getinstance (), Only the unique instance is created in memory. It also solves the problem of multi-threading in lazy-type. The solution is to take advantage of the ClassLoader features to achieve thread safety and avoid the performance impact of synchronization. Public class singleton
Java design mode singleton mode (Singleton) [reprint]Reprint Please specify source: http://cantellow.iteye.com/blog/838473ObjectiveLazy: create an object only when calledA hungry Man: object is created when class is initializedThe first type (lazy, thread insecure):1 public classSingleton {2 Private StaticSingleton instance; 3 PrivateSingleton () {}4 5
-case mode conditions:There is a requirement for using singleton mode: A singleton pattern should be used when a system requires only one instance of a class, and conversely, if a class can have several instances coexisting, then it is not necessary to use a singleton pattern class.4 single cases in Java in 3 (general
1. DefinitionSingleton mode (Singleton), also known as the list mode, is a common software design pattern. When you apply this pattern, the class of the Singleton object must guarantee that only one instance exists. Many times the entire system needs to have only one global object, which helps us coordinate the overall behavior of the system. For example, in a se
latter, and using singleton can extend the base class through inheritance and polymorphism, realize interface and more ability to provide different implementations, if we discuss Java.lang.Runtime, in Java it is a singleton, call GetRuntime () method, which returns different implementations based on different JVMs, but also guarantees that there is an instance i
First, describeSingleton mode is a very common design pattern in which a class can have only one object (instance), typically by privatizing the class's constructor to prevent the object from being created outside of the class and providing a unique object to the outside world (this object is created in the Class). There are two common implementations of the singleton pattern in Java, one of which is the Ba
singleton2{
private static Singleton Ourinstance = new Singleton ();
}
Private Singleton () {
} public
static Singleton newinstance () {
Return singleton2.ourinstance
}
}
Solution 4: Enum type
The simplest and easiest way to implement a single example
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
the following code is thread-safe:Public class Singleton3 {private static volatile Singleton3 instance = null;Private Singleton3 () {}Public static Singleton3 getinstance () {if (instance = = null) {synchronzied (singleton3.class) {if (instance = = null) {instance = new Singleton3 (); } } }return instance; }}Singleton3 uses a locking mechanism to ensure that only one instance is created in a multithreaded environment, and that the two if judgments are used to improve efficiency. This
an element is an instance. */INSTANCE; /*** Test Method 1 *@return */ Public voiddosomething () {System.out.println ("# # # #测试方法 ######"); } /*** Test Method 2 *@return */ PublicString getsomething () {return"Got some content."; }}In the example above, enumsingleton.instance can obtain the desired instance, and the method of invoking the singleton can be EnumSingleotn.INSTANCE.doSomeThi
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.A singleton class can have only one instance.The Singleton class must create its own unique instance.The Singleton class must provide this instance to all
system will regard it as garbage and will automatically destroy and Recycle resources. The next exploitation will be re-instantiated, which will lead to the loss of the shared singleton object status.
3. Applicable scenarios
The Singleton mode can be considered in the following cases:
(1) The system only needs one instance object. For example, the system require
This article address: http://www.cnblogs.com/archimedes/p/java-singleton-pattern.html, reprint please indicate source address.
Single-Case mode
Ensure that a class has only one instance and provides a global access point to access it.
OverviewThe singleton pattern is a mature pattern of how to design a class that has only one instance of the cla
PackageCom.easygo.singleton;/*** Java Singleton There are two ways, a hungry man and lazy, a hungry man is loaded before the object is created, take precedence over the object, and the lazy is to call the object after the object creation method to create the object *, understand the JVM loading principle is clear, the true meaning of the singleton is a hungry man
role, it includes business methods that combine the creation of products with the functionality of the product itself.
Many object-oriented languages, such as Java, C #, now run environments that provide automatic garbage collection, so if the instantiated shared object is not exploited for a long time, the system will consider it garbage, automatically destroy and reclaim the resources, and re-instantiate the next time it is exploited, which wi
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.