Introduction to the application and advantages of the single example model of Android source learning _android

Source: Internet
Author: User
Tags static class stub volatile
single case mode definition
Ensure a class has only one instance, and provide a global point of access to it.
Dynamically ensures that a class has only one instance and instantiates it itself and provides this instance to the entire system.

As shown in the figure above (intercepts the book from the head of Patterns).


Using the private constructor ensures that an instance is generated in an application and is instantiated (using the new Singleton () itself in Singleton).
What are the advantages of a specific single case pattern?
Because Singleton mode has only one instance in memory, the memory overhead is reduced.
The singleton pattern avoids multiple usage of resources, such as when a file is written, so that only one instance is in memory, avoiding simultaneous write operations on the same resource file.
The single case mode allows the system to set global access points, optimizing and sharing resource access.
The use of a single example mode, consider more is multithreading in the case of how to prevent the simultaneous creation of multithreading and other issues, of which "head of the Patterns" used to "double-checked locking" To reduce the use of synchronization.
Copy Code code as follows:

public class Singleton {
/* The volatile keyword ensures that multiple threads
* Handle the Uniqueinstance variable correctly when it
* is being initialized to the Singleton instance.
* */
Private volatile static Singleton uniqueinstance;
Private Singleton () {}
public static Singleton getinstance () {
if (uniqueinstance = = null) {
Synchronized (Singleton.class) {
if (uniqueinstance = = null) {
Uniqueinstance = new Singleton ();
}
}
}
return uniqueinstance;
}
}

In the Android code, there are a number of examples that use a single example pattern:
one, such as Inputmethodmanager class
Copy Code code as follows:

Public final class Inputmethodmanager {
Static Final Boolean DEBUG = false;
Static final String TAG = "Inputmethodmanager";
Static final Object Minstancesync = new Object ();
Static Inputmethodmanager minstance;
Final Iinputmethodmanager Mservice;
Final Looper Mmainlooper;

Create a unique instance static Inputmethodmanager minstance;
Copy Code code as follows:

/**
* Retrieve The global Inputmethodmanager instance, creating it if it
* doesn ' t already exist.
* @hide
*/
static public Inputmethodmanager getinstance (context context) {
Return getinstance (Context.getmainlooper ());
}
/**
* Internally, the Input method manager can ' t is context-dependent, so
* We have this to the places that need it.
* @hide
*/
static public Inputmethodmanager getinstance (Looper mainlooper) {
Synchronized (Minstancesync) {
if (minstance!= null) {
return minstance;
}
IBinder B = Servicemanager.getservice (Context.input_method_service);
Iinputmethodmanager service = IInputMethodManager.Stub.asInterface (b);
Minstance = new Inputmethodmanager (service, mainlooper);
}
return minstance;
}

prevent multithreading from creating instances at the same time
Copy Code code as follows:

Synchronized (Minstancesync) {
if (minstance!= null) {
return minstance;
}

When no instance object is created, the call minstance = new Inputmethodmanager (service, mainlooper);
Where the class constructor looks like this:
Copy Code code as follows:

Inputmethodmanager (Iinputmethodmanager service, Looper looper) {
Mservice = Service;
Mmainlooper = Looper;
MH = new H (looper);
Miinputcontext = new Controlledinputconnectionwrapper (Looper,
Mdummyinputconnection);
if (minstance = = null) {
Minstance = this;
}
}

Second, Bluetoothoppmanager class
Copy Code code as follows:

public class Bluetoothoppmanager {
private static final String TAG = "Bluetoothoppmanager";
Private static Final Boolean V = Constants.verbose;
To create an instance of a private static class
private static Bluetoothoppmanager INSTANCE;
/** Used When obtaining a reference to the singleton instance. */
private static Object Instance_lock = new Object ();
。。。
/**
* Get Singleton instance.
*/
public static Bluetoothoppmanager getinstance (context context) {
Synchronized (Instance_lock) {
if (INSTANCE = = null) {
INSTANCE = new Bluetoothoppmanager ();
}
Instance.init (context);
return INSTANCE;
}
}

Third, Accessibilitymanager class
Copy Code code as follows:

Public final class Accessibilitymanager {
Private static Final Boolean DEBUG = false;
private static final String Log_tag = "Accessibilitymanager";
/** @hide * *
public static final int state_flag_accessibility_enabled = 0x00000001;
/** @hide * *
public static final int state_flag_touch_exploration_enabled = 0x00000002;
Static final Object Sinstancesync = new Object ();
private static Accessibilitymanager sinstance;
...
/**
* Get an Accessibilitymanager instance (create one if necessary).
*
* @hide
*/
public static Accessibilitymanager getinstance (context context) {
Synchronized (Sinstancesync) {
if (sinstance = = null) {
IBinder IBinder = Servicemanager.getservice (Context.accessibility_service);
Iaccessibilitymanager service = IAccessibilityManager.Stub.asInterface (IBinder);
Sinstance = new Accessibilitymanager (context, service);
}
}
return sinstance;
}
/**
* Create an instance.
*
* @param context A {@link context}.
* @param service an interface to the backing service.
*
* @hide
*/
Public Accessibilitymanager (context, Iaccessibilitymanager service) {
Mhandler = new MyHandler (Context.getmainlooper ());
Mservice = Service;
try {
Final int stateflags = mservice.addclient (mclient);
SetState (StateFlags);
catch (RemoteException re) {
LOG.E (Log_tag, "Accessibilitymanagerservice is Dead", re);
}
}

Wait a minute...
The beginning of the first week of the new year, from the simplest single example to start recording your learning process it ~ ~ ~
Related Article

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.