Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Design pattern-- singleton pattern-- first day of study

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Singleton mode

Advantages of the singleton model:

Because the singleton mode generates only one instance, which reduces the system performance overhead, when an object needs more resources, such as reading configuration and generating other dependent objects, it can be solved by directly generating a singleton object at startup and then permanently residing in memory.

Implementation of singleton pattern:

There are five ways.

1. Hungry Han style

two。 Lazy style

3. Implementation of double detection lock

4. Static inner class implementation

5. Enumerated type implementation

The following is to make detailed notes on the types of the five middle schools.

Hungry Han style singleton model

Hungry Chinese style features: thread safety, high call efficiency, but can not delay loading

Disadvantages: if you just load the class without calling the methods in the class, it will result in a waste of resources

The realization of hungry Chinese style:

1. Define a static variable in the definition class, and then create a class object to assign values to the static variable

two。 The constructor will be privatized.

3. Define static methods to return static variables

Code:

When the public class SingletonDemo1 {/ / class initializes, load this object immediately (without the advantage of delayed loading). When loading classes, it is naturally thread-safe! Private static SingletonDemo1 instance = new SingletonDemo1 (); private SingletonDemo1 () {} / / method is not synchronized, so it is efficient to call! Public static SingletonDemo1 getInstance () {return instance;}}

Lazy singleton model

Lazy features: thread safety, low call efficiency, but can delay loading

Disadvantages: resource utilization is high, but every call to the getInstance () method has to be synchronized, resulting in low efficiency of concurrency.

Lazy realization:

1. Define a private static variable in the defined class without assignment

two。 Privatization of construction method

3. Lock synchronized to the getInstance () method, create an object only once in the method, and assign a value to the static variable once

Code:

When the public class SingletonDemo2 {/ / class initializes, the object is not initialized (deferred loading, created when you actually use it). Private static SingletonDemo2 instance; private SingletonDemo2 () {/ / privatize constructor} / / method synchronization, low call efficiency! Public static synchronized SingletonDemo2 getInstance () {if (instance==null) {instance= new SingletonDemo2 ();} return instance;}}

A single example of double detection lock realization

Dual detection lock features: lazy loading and high call efficiency are realized.

Disadvantages: due to compiler optimization and jvm underlying internal model reasons, occasional problems occur and are not recommended

Double detection lock implementation:

Public class SingletonDemo3 {private static SingletonDemo3 instance = null; public static SingletonDemo3 getInstance () {if (instance = = null) {SingletonDemo3 sc; synchronized (SingletonDemo3.class) {sc = instance; if (sc = = null) {synchronized (SingletonDemo3.class) {if (sc = = null) {sc = new SingletonDemo3 () } instance = sc;} return instance;} private SingletonDemo3 () {}}

Static inner class implementation singleton

Static inner class features: thread safety, high call efficiency, lazy loading

Static inner class singleton implementation:

1. Define a static inner class in the defined class, define a static constant in the static inner class, and create an object to assign values to the static constant.

two。 Privatization of construction method

3. Define the getInstance () method to return static constants defined in static inner classes

Code:

The public class SingletonDemo4 {private static class SingletonClassInstance {private static final SingletonDemo4 instance = new SingletonDemo4 ();} private SingletonDemo4 () {} / / method is not synchronized and the call is efficient! Public static SingletonDemo4 getInstance () {return SingletonClassInstance.instance;}}

Enumerate a single example of implementation

Enumeration implementation features: thread safety, no lazy loading, high call efficiency, simple implementation. Enumeration itself is a singleton mode, which is fundamentally guaranteed by JVM to avoid loopholes through reflection and deserialization.

Disadvantages: no delay loading

Enumerated implementation singleton:

Define enumerated classes

Define an element in the enumeration class

Define to return the elements defined in the enumeration

Code:

Public enum SingletonDemo5 {/ / this enumeration element is itself a singleton object! INSTANCE; / / add the actions you need! Public void singletonOperation () {System.out.println ("enumerate to implement singleton pattern");}}

Summary of singleton pattern

Mainly:

Hungry Chinese style (thread safety, high call efficiency, can not delay loading)

Lazy (thread-safe, inefficient, and can delay loading)

Other

Double check lock (due to the jvm underlying internal model prototype, there are occasional problems, not recommended)

Static inner class (thread-safe, efficient invocation, deferred loading)

Enumerated (thread-safe, efficient call, can not delay loading, and can naturally prevent reflection and deserialization vulnerabilities)

How to choose?

The singleton object takes up less resources and does not need delayed loading.

Enumeration is better than hungry Han style.

The singleton object takes up a lot of resources and requires delayed loading.

Static inner classes are better than lazy ones.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report