Detailed explanation of single example mode of Android design mode
Singleton mode
A class has only one instance and can be accessed and used globally
Application scenario
Such as account management class, database operation class, etc. (an object is frequently accessed and used)
Common way
Hungry lazy synchronous locking DCL double locking verification static internal class enumeration singleton
Initialization is performed immediately while loading classes, which consumes a lot of resources
Lazy style
Advantages: initialization loading is performed only when it needs to be used
Disadvantages: threads are unsafe, and it is easy to be out of sync in multithreading
Synchronous locking
Advantages: solve thread safety problems
Disadvantages: each instance needs to judge the locking state, which is inefficient
DCL double check
Advantages: it can run perfectly when the concurrency is not high (recommended, there is almost no high concurrency on the client)
Disadvantages: before JDK 1.5, the instance initialization problem may occur (now Android is almost 1.7 and 1.8, which can be ignored)
Static inner class
Advantages: delayed loading, thread safety, less memory consumption (recommended)
Enumeration singleton
Advantages: thread safety, anti deserialization, anti reflection, simple writing
GitHub code address
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.