Singleton mode is used in Android

Source: Internet
Author: User

Singleton mode (Singleton)

Singleton mode is the creation mode of an object, and the singleton pattern ensures that a class has only one single instance object, and can instantiate itself and provide a single instance to the outside world, which is often used in the actual project development.

The structure diagram of the singleton pattern is illustrated below:

Combining the above pattern structure diagram, we can see that the singleton pattern has several notable features, as follows:

A, the Singleton class (Singleton) can only have one unique instance.

B, the Singleton class must have the ability to create its own instance object itself.

C, the Singleton class must be able to provide this instance to other outside objects.

In addition, for the singleton pattern from the generation to the present, there are several forms, below we introduce several commonly used forms (in the form of code description):

The first form of:

/*

 * single-case mode--- form 1

*/

Public class Singletona {

private static final Singletona Instance =new Singletona ();

/ * Private Construction Child * /

Private Singletona () {

//CODE

}

/ * instance method for external invocation * /

Public static Singletona getinstance() {

return instance;

}

}

The second form of:

/*

 * single-case mode--- form 2

*/

Public class SINGLETONB {

private static Singletonb instance =null;

/ * Private Construction Child * /

Private Singletonb () {

//CODE

}

/ * instance method for external invocation * /

Public static Singletonb getinstance () {

if (null = =instance) {

instance = new singletonb ();

}

return instance;

}

}

The third form of:

/*

 * single-case mode--- form 3

*/

Public class Singletonc {

private static Singletonc instance =null;

/ * Private Construction Child * /

Private Singletonc () {

//CODE

}

/ * The instance method for external invocation is used here in sync * /

Public static synchronized Singletonc getinstance () {

if (null = =instance) {

instance = new singletonc ();

}

return instance;

}

}

Of course, here I only listed the more common forms of the Singleton, below to specifically analyze the relationship between them and the difference, and at the end of a practical example to use the singleton mode. The following are the specific examples:

The characteristic of the singleton pattern for form 1 is that it instantiates itself when the Singleton class itself is loaded. But from the point of view of resource utilization, the implementation of this approach will be almost the same, while the second form of Singleton mode is to optimize the first form of the small disadvantage, that is, when the loader loads the singleton class does not choose to create the instance object, only when the case is really needed to create a unique instance If viewed from the speed of time and reaction, then the form 1 of the singleton is faster than the form 2. For the singleton of Form 3, because it uses the mechanism of synchronization, it is bound to reduce its performance, advantages and disadvantages, it can be good in the asynchronous thread to ensure that the uniqueness of the instance.

In general, Form 1 and Form 2 are more in line with the Java mechanism, as I am personally accustomed to the second form of singleton mode. Here is an example to illustrate the use of the singleton, of course, I use my own habit of the singleton pattern form. The example is this, in the actual project, I will often want to get mobile device screen width and high, so for the sake of management and unification, we generally choose to get the width and height of the method encapsulated in a single class, and in order to avoid the duplication of the creation of this class, we choose a singleton pattern to achieve. My code is as follows:

Single-Case mode:

/* Singleton class * /

Public class Phoneutil {

private static Phoneutil instance =null;

private static Context Context =null;

/ * Private Construction Child * /

Private Phoneutil (Context ctx) {

context = CTX;

}

/ * The instantiation method for external invocation guarantees the creation of a unique instance object * /

Public static Phoneutil getinstance (contextctx) {

if (null = =instance) {

instance = new phoneutil (CTX);

}

return instance;

}

/ * Get the width of the screen * /

Public int getscreenwidth () {

Display display = ((WindowManager)

context. Getsystemservice (context. Window_service)). Getdefaultdisplay ();

return display.getwidth ();

}

/ * Get the height of the screen * /

Public int getscreenheight () {

Display display = ((WindowManager)

context. Getsystemservice (context. Window_service)). Getdefaultdisplay ();

return display.getheight ();

}

}

Front-end invocation:

Public class Singletonactivity extends Activity {

private static String TAG = "Singletonactivity";

@Override

protected void onCreate (bundlesavedinstancestate) {

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Activity_singleton);

int screenwidth = phoneutil. getinstance (this). Getscreenwidth ();

Log ("The screen width:" + screenwidth);

int screenheight = phoneutil. getinstance (this). Getscreenheight ();

Log ("The screen height:" + screenheight);

}

private void log (String log) {

Log. D (TAG, log);

}

}

How the code works:

From the above figure, my phone's screen width and height are 320 and 480 respectively, it seems the resolution and the screen small poor!

Well, here we have a basic analysis and use of common singleton patterns.

Technology Exchange Group:179914858

Singleton mode is used in Android

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.