Java design Pattern--Singleton mode

Source: Internet
Author: User

Just want to write something about design patterns, but lazy to now have not written anything, this morning to see the code in the project, in this noon time to write something, throw a project first:

Single-Case mode:

Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that the class with the pattern can have only one instance of the class that is used in the system. That is, a class has only one instance of an object.

The following is a list of common singleton patterns in Java:

First, lazy mode

This kind of writing seems to implement a singleton pattern, but goose, practical application is not practical, although I am in the project has a similar code, I have to say that is true;

Pros: Create on-demand, lazy loading

Disadvantage: Thread is not secure. In a multithreaded case, this approach does not fully implement the singleton pattern. For example, when multiple threads access the Getsingleton method, a null = = Singleton is determined together, and instances of multiple singleton are created.

Implement a thread-safe lazy mode (two different ways, but consistent):

The above two kinds of writing although have the difference, but the operation effect is the same;

Advantages: Solve the security problem of multithreading

Disadvantage: The efficiency is low do not want. Because threads are queued whenever a thread is accessed, there is only one thread that actually creates the instance.

In reality, some people also restrict object instances by adding the volatile keyword, which guarantees the visibility of all threads, and prohibits the optimization of command reordering.

Here's a lazy model that takes into account thread safety and efficiency:

This approach, known as double-check lock/double-lock mode, is a two-time null operation that resolves a thread-safe issue and does not create multiple instances. At the same time, it also improves the efficiency of thread access. This method can be counted as the optimal solution of the lazy model, but it is not the optimal solution of a singleton pattern.

Second, a hungry man mode

Advantages: Simple writing, at a glance

Cons: Unable to create on demand, that is lazy loading, increase load.

Note : The additional load here depends on the specific situation, in terms of the above code, new a singleton instance, not add load, but if you new is a large object, then you should consider this problem.

Third, static internal class

Put the singleton instance into a static inner class, which avoids the creation of the static instance when the Singleton class is loaded, and because the static inner class is only loaded once, this is also thread-safe.

Pros: Thread-safe, create on-demand.

Cons: 1, additional work is required (Serializable, transient, readresolve ()) to serialize, or a new instance is created each time a serialized object instance is deserialized.

2. You might be forced to call our private constructor with reflection (if you want to avoid this, you can modify the constructor so that it throws an exception when creating the second instance).

Iv. Enumeration

The use of enumerations, in addition to thread-safe and anti-reflection forced invocation of the constructor, also provides an automatic serialization mechanism to prevent deserialization when a new object is created. Therefore,effective Java recommends using enumerations as much as possible to implement the singleton.

Finally, regardless of the approach, keep in mind the three main points of the single case:

    • Thread Safety
    • Lazy Loading
    • Serialization and deserialization security

Java design Pattern--Singleton 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.