Android design Model of the single example mode detailed _android

Source: Internet
Author: User

This is our most common type of pattern, and one of the common features of this type of pattern is:

Encapsulates the way and process of creating.

The so-called encapsulation is hidden meaning, the object creation method and process is not visible, or virtual process.

The hidden creation method is like a single example, the factory method, the hidden creation process refers to builder, the prototype, as for the abstract factory, I think he contains the above two kinds.

What steps do we have to create an object?

1. What do you create? --Interface Definition

2, who created? --decision class or help class

3, how to create? --how, creating a process

4, when to create? --Trigger to create time

So, the creation type is to make a fuss over a few points

I put the single case pattern first, because it's simple and straightforward.

1. Single Case mode

Gof the original words:

Copy Code code as follows:

Ensure a class only has one instance, and Providea global point of access to it

There are 2 points:

A, only and only 1 instances

b, providing a global access point

This means that an object can only be generated once, and then the global method or class is invoked.

< something to leave, to be continued >

As you can see from the above description, we typically use this pattern when we need only one instance of an object, like the global object we often say, in Java EE we know that the default spring initialization bean is a singleton, and we can define it in the configuration file as follows:

Copy Code code as follows:

<bean id= "foo" class= "foo" singleton= "true"/>

This tells the spring container that the instance of Foo will only be generated once.

So where does Android go with a single case pattern?

We know that a mobile phone, open the input method, regardless of where to open, in fact, is an example There is a Msearchmanager object in Activity.java It is also a single instance object; If it's an Android native system, there's a global search, and if you look at the Android source, you'll find Displaymanagerglobal,windowmanage Rglobal and so many are single cases, these objects are responsible for managing the entire operation of the mobile phone processing. Let's look at the implementation of Windowmanagerglobal:

Copy Code code as follows:

public static Windowmanagerglobal getinstance () {
Synchronized (Windowmanagerglobal.class) {
if (Sdefaultwindowmanager = = null) {
Sdefaultwindowmanager = new Windowmanagerglobal ();
}
return sdefaultwindowmanager;
}
}

This system guarantees that the Windowmanagerglobal object will produce only one, call getinstance (Global access point) to generate new when the system call (the decision object) is needed. This is a very complete single example of the implementation of the model, a good example.

Msearchmanager's implementation is also interesting:

Copy Code code as follows:

private void Ensuresearchmanager () {
if (Msearchmanager!= null) {
Return
}

Msearchmanager = new Searchmanager (this, null);
}

There are people here who would say, well, no return Searchmanager object, that is, there is no decision-class role. Actually he has, his decision class is our commonly used getsystemservice, looks at the code:

Copy Code code as follows:

@Override
Public Object Getsystemservice (String name) {
if (getbasecontext () = null) {
throw New IllegalStateException (
"System services not available to activities before OnCreate ()");
}

if (window_service.equals (name)) {
return mwindowmanager;
else if (search_service.equals (name)) {
Ensuresearchmanager ();
return msearchmanager;
}
return Super.getsystemservice (name);
}

In fact here Getsystemservice we can see as a special decision class, from the following code:

Copy Code code as follows:

if (search_service.equals (name)) {
Ensuresearchmanager ();
return msearchmanager;
}

We change it to:

Copy Code code as follows:

public static Searchmanager getinstance () {
Ensuresearchmanager ();
return msearchmanager;
}

This is a single example pattern. But looking at the whole code from Getsystemservice, the factory method pattern is true, as we'll talk about here.

Depending on the timing of the object creation, there are three ways to single case patterns:

1, a hungry man--the instance is generated when the class is loaded

Copy Code code as follows:

public class foo{
Foo () {}
private static Foo instance =new foo ();
public static Foo getlnstance () {
return instance;
}
}

2, lazy type

Copy Code code as follows:

public class foo{
Foo () {}
private static Foo instance = NULL;
public static Foo getlnstance () {
if (instance = = null) {
instance = new Foo ();
}
return instance;
}
}

3. Registration type

Copy Code code as follows:

public static foo getinstance (String name) {
if (name = = null) {
name = foo. Class.getname ();
System.out.println ("name = = NULL" + "--->name=" +name);
}
if (map.get (name) = null) {
try {
Map.put (name, (foo) class.forname (name). newinstance ());
catch (Instantiationexception e) {
E.printstacktrace ();
catch (Illegalaccessexception e) {
E.printstacktrace ();
catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}
return Map.get (name);
}


The first and second methods differ in the timing of the creation, and the third is how to create the difference.

Finally, ask a few questions:

1. What other single case mode is Android

2, launcher mode in the implementation of a single case

3, the expansion of the single case mode, the database connection pool belongs to which way variation (derivation).

What are the advantages and disadvantages of a single example pattern in 4 or three ways? What is the general purpose difference?

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.