Android2.3.5-Based System: C ++ Singleton mode of native layer in Android

Source: Internet
Author: User

**************************************** **************************************** **************************************** ***
Author: EasyWave time: 2013.02.16

Category: Android source code analysis statement: reprinted. Please keep the link

NOTE: If any error occurs, please correct it. These are my Learning Log articles ......

**************************************** **************************************** **************************************** ***

I. What is Singleton mode?

The Singleton mode is also called the singleton mode and the singleton mode. It may be the most widely used design mode. The intention is to ensure that a class has only one instance and provides a global access point for it. This instance is shared by all program modules. The Singleton mode has many implementation methods. In C ++, you can even use a global variable to achieve this. You can use a global object to easily access the instance, but you cannot declare only one object. That is to say, you can still create local instances of the same class except for one global instance. The Chinese version of design patterns provides a very good implementation. On the 84 pages, I started to talk about this Singleton pattern. The specifics can be described in the reference books.

Ii. Advantages of Singleton Mode

1) controlled access to a unique instance

Because the Singleton class encapsulates its unique instance, it can strictly control how and when customers access it.
2) narrow down the namespace

The Singleton mode is an improvement for global variables. It prevents global variables that store unique instances from polluting namespace.
3) allows refinement of operations and representations

The Singleton class can have child classes, and it is easy to configure an application using the instance of this extension class. You can use the instance of the class you need to configure the application at runtime.
4) Allow variable target instances

This mode makes it easy for you to change your mind and allow multiple instances of the Singleton class. In addition, you can use the same method to control the number of instances used by the application. Only the operations that allow access to the Singleton instance need to be changed.
5) more flexible than class operations

Another way to encapsulate a single-piece function is to use class operations (static member functions in C ++ or class methods in Smalltalk ). However, the two language technologies cannot change the design to allow a class to have multiple instances. In addition, static member functions in C ++ are not virtual functions, so subclasses cannot be redefined with polymorphism.

Iii. Singleton Mode Implemented in Android

In the Android system, we can see this design pattern in many places, such as the ProcessState class and IPCThreadState class. Let's take a look at their implementation code. In the frameworks \ base \ libs \ binder \ ProcessState. cpp file, we can see the following definitions:

Sp <ProcessState> self () is defined in the frameworks \ base \ include \ binde \ ProcessState. h file, as follows:

Take a closer look at the section in the red circle. From the code above, we can see that this is a typical C ++ Singleton mode, and gProcess is in Static. as defined in cpp, only the self () member function is used to access a unique instance. If you do not use this function, any attempt to create an instance will fail because the class constructor is private. Self () uses lazy initialization, that is, its return value is created when this function is accessed for the first time. This is a bulletproof design-all calls after self () return the pointer of the same instance. How is it called? [In frameworks \ base \ cmds \ app_process \ App_main.cpp]

In Andriod, there is another place, which needs to be mentioned here, that is, in frameworks \ base \ libs \ binder \ IPCThreadState. in the cpp file, this class function set about the inter-process communication of the binder. The call method uses TLS, that is, Thread Local Storage (TLS ). Andriod is a multi-threaded system. In addition to sharing process resources, each thread also has its own private resources:
A register group (or thread context );
An exclusive stack;
An exclusive message queue;
A dedicated Thread Local Storage (TLS );
A dedicated string of structured exception handling.

Here we will focus on Thread Local Storage. Let's take a look at the specific code implementation, as shown below:

Yes, pthread_getspecific () and pthread_key_create () functions are TLS-related functions. For details about the thread concept, refer:POSIX multi-thread programmingThis book. This book explains in detail the concept of threads in Linux. TLS is used as the unique instance for user access. This place is very clever ......

How to call it? See the static void android_ OS _Parcel_enforceInterface (JNIEnv * env, jobject clazz, jstring name) function in frameworks \ base \ core \ jni \ android_util_Binder.cpp, as follows:

In the Andriod source code, many places adopt reusable object-oriented software design patterns. Therefore, it is recommended that you first take a look at the design mode final solution-GoF 23 design mode parsing with C ++ implementation source code, which can be downloaded at the address below, divided into C ++ and Java:

C ++ version:Http://wenku.baidu.com/view/69b6c04b2b160b4e767fcff5.html

Java version:Http://wenku.baidu.com/view/bbf5e9d633d4b14e85246884.html 

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.