The of Android development design pattern — detailed explanation of single example pattern

Singleton pattern is the most common and simplest design pattern in design patterns, which ensures that only one instance exists in the program and can be accessed globally. For example, the account information object management and database object (sqliteopenhelper) used in the actual android app development will use the singleton mode. Here are some examples to analyze the points we need to pay attention to when applying the singleton pattern in the development process.

1、 Function

Singleton: ensure that a class has only one instance and provide a global access point to access it

2、 Applicable scenario

1. An instance object in an application needs to be accessed frequently.

2. In the application, only one instance will exist each time it is started. Such as account system and database system.

3、 Common usage

(1) Lazy style

This is an easy way to write in development, as follows

Call:

Advantages: delayed loading (loading only when needed)

Disadvantages: the thread is unsafe. It is easy to be out of sync in multithreading, such as frequent read and write operations on database objects.

(2) Add synchronous lock

Since the thread is unsafe, add a synchronization lock. An addition is as follows:

More generally, it is written like this

Call:

Advantages: it solves the problem of thread insecurity.

Disadvantages: the efficiency is a little low. You have to judge the synchronization lock every time you call the instance

Supplement: the singleton methods used in the Android source code are: inputmethodmanager, accessibilitymanager, etc. all use this singleton mode

(3) Double check lock

To optimize the problem in (2), many people use the following double judgment verification method because they have to judge the synchronization lock every time they call the instance

This method seems to perfectly solve the above efficiency problems. It may run perfectly when there is not much concurrency and security. However, this method also has unfortunate places. The problem is in this sentence

During JVM compilation, there will be an optimization process of instruction rearrangement, which will lead to the allocation of memory space when instance has not been initialized, that is, instance= Null but not initialized, which will cause the returned instance to be incomplete

Call:

Advantages: the singleton mode may run perfectly when the concurrency is small and the security is not high

Disadvantages: there may be serious security risks during compilation on different platforms.

Add: this method is used in Android universal image loader, an Android image open source project.

(4) Implementation of internal classes

Internal classes are a good implementation method. The following are recommended:

Call:

Advantages: delayed loading, thread safety (mutually exclusive when loading classes in Java), and reduced memory consumption

(5) Enumeration method

This is a practice recommended by many people on the Internet, but it seems that it is not widely used. You can try it,

Call:

Advantages and disadvantages: such as comments in the code.

The above mainly describes the five creation methods of singleton mode, which can be used in personal actual projects according to their advantages and disadvantages. It's about throwing bricks and attracting jade. Let's put forward more opinions.

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
分享
二维码
< <上一篇
下一篇>>