Fully analyze the singleton pattern of Java design pattern

This example shares the specific code of the singleton pattern of Java design pattern for your reference. The specific contents are as follows

Concept:

Singleton pattern: there is only one instance in a class.

A class has only one instance and provides a global access point.

Reasons for using this mode:

When we browse websites, some websites will display "current online people". Usually, the way to realize this function is to store each IP logged in in a memory, file or database. For each additional IP, "+ 1" will be realized. Generally, a method, such as add (), is used to realize the function of "+ 1". For example, the "update" statement is used to obtain the data stored in the database, then + 1, update the data in the database, and then save; When displayed on the page, you can obtain the data in the database through another method. However, when multiple users log in at the same time, if each needs to create a new object, then call and execute the add () method through the "object. Method name", and then store the data in the database, which will cause multiple users to be unable to accurately record the actual user data in the database. Therefore, designing this counter as a global object (everyone uses this object instead of a new one) and everyone shares the same data can avoid similar problems. This is one of the applications of what we call singleton mode.

Similarly, in other scenarios, similar scenarios will be encountered and similar ideas will be used. For example:

   1. External resources: each computer has several printers, but only one printerspooler to avoid two print jobs being output to the printer at the same time. Internal resources: most software has one (or more) attribute files to store system configuration. Such a system should have an object to manage these attribute files. 2. Windows Task Manager is a typical singleton mode (this is familiar). Think about it. Can you open two windows task managers? If you don't believe it, try it yourself ~ 3. Recycle bin of windows (Recycle Bin) is also a typical singleton application. During the operation of the whole system, the recycle bin maintains only one instance. 4. The counters of the website are generally implemented in singleton mode, otherwise it is difficult to synchronize. 5. The log application of the application is generally implemented in singleton mode, which is generally because the shared log files are always playing On status, because there can only be one instance to operate, otherwise the content is not easy to append.    6. The singleton mode is generally applied to the reading of configuration objects of web applications because the configuration file is a shared resource.    7. The design of database connection pool generally adopts single instance mode, because database connection is a kind of database resource. The use of database connection pool in database software system is mainly to save the efficiency loss caused by opening or closing database connection. This efficiency loss is still very expensive, because it can be greatly reduced by using singleton mode for maintenance.    8. The design of multithreaded thread pool generally adopts single instance mode, because the thread pool should facilitate the control of threads in the pool.    9. The file system of the operating system is also a specific example of the implementation of large singleton mode. An operating system can only have one file system.    10. Httpapplication is also a typical application of unit example. Familiar with ASP Net (IIS) throughout the request life cycle, people should know that httpapplication is also a singleton mode, and all httpapplication modules share an httpapplication instance.

To sum up, the general application scenarios of singleton mode are:

   1. Objects that need to be instantiated frequently and then destroyed.

   2. Objects that take too much time or resources to create, but are often used.

  3. Stateful tool class object.

   4. Objects that frequently access databases or files.

   5. In the case of resource sharing, avoid performance or loss caused by resource operation. Such as log file, application configuration, etc.

   6. In the case of controlling resources, it is convenient for resources to communicate with each other. Such as thread pool.

characteristic:

1. A singleton class can only have one instance;

2. A singleton class must create its own unique instance;

3. A singleton class must provide this instance to all other objects

Singleton mode elements:

1. Private construction method 2 Private static reference points to its own instance 3 A public static method with its own instance as the return value

There are three ways to implement singleton mode:

1. Hungry Chinese style: a single instance is built and initialized when the class is loaded. (preloading method)

advantage

1. Thread safety 2 A static object has been created while the class is loaded, and the reaction speed is fast when calling

shortcoming

The resource efficiency is not high. Maybe getInstance () will never be executed, but if other static methods of the class are executed or the class (class. Forname) is loaded, the instance will still be initialized

2. Lazy: the singleton instance is built when it is used for the first time, and the initialization is delayed.

advantage

It avoids the hungry Chinese way of creating an instance when it is not used. It has high resource utilization. If getInstance () is not executed, it will not be instantiated. Other static methods of this class can be executed.

shortcoming

The lazy type has no problem in a single thread, but when colleagues from multiple threads access it, they may create multiple instances, and these instances are not the same object. Although the later created instances will overwrite the first created instances, they will still get different objects. The solution to this problem is to lock synchonized. The first load is not fast enough, and the unnecessary synchronization overhead of multithreading is high.

3. Double detection

advantage

The resource utilization is high. If you do not execute getinstance(), you will not be instantiated. You can execute other static methods of this class

shortcoming

The response is not fast when loading for the first time, and it occasionally fails due to some reasons of JAVA memory model

4. Static internal class

advantage

High resource utilization. If getInstance () is not executed, it will not be instantiated. Other static methods of this class can be executed

shortcoming

The reaction is not fast enough at the first load

Summary:

Generally, the hungry type is used. If you are very concerned about resources, you can use static internal classes. Lazy type and double detection are not recommended

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>