Android [advanced tutorial] Seven Singleton mode in Design Mode

Source: Internet
Author: User

The Singleton mode is also my favorite mode, because no matter how many objects you create, the current object only has one instance in the memory, and operations on the database like Android, HTTP requests can all be implemented in the singleton mode, and the efficiency will be improved a lot. Here we will implement the singleton model for the hero Tang Miao in Journey to the West:

[Java] <pre name = "code" class = "java"> public class Tangseng {
 
Private static Tangseng tangseng;
 
Tangseng (){
// TODO Auto-generated constructor stub
}
 
Public Tangseng getInstance (){
 
If (tangseng = null ){
Tangseng = new Tangseng ();
}
 
Return tangseng;
 
}
 
}
<Pre name = "code" class = "java"> public class Tangseng {

Private static Tangseng tangseng;

Tangseng (){
// TODO Auto-generated constructor stub
}

Public Tangseng getInstance (){

If (tangseng = null ){
Tangseng = new Tangseng ();
}

Return tangseng;

}

}

 

 

With just a few simple lines of code, we have implemented the singleton mode. No matter how many new objects you call in the future, there will be only one in the memory.

In Android, let's take a look,


[Java] public class XiyoujiActivity extends Activity {
 
Private ListView listView;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
ListView = (ListView) findViewById (R. id. listView1 );
ArrayList <String> lists = new ArrayList <String> ();
 
Lists. add (new Tangseng (). getInstance (). toString ());
Lists. add (new Tangseng (). getInstance (). toString ());
 
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, lists );
 
ListView. setAdapter (adapter );
 
}
}
Public class XiyoujiActivity extends Activity {

Private ListView listView;

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

ListView = (ListView) findViewById (R. id. listView1 );
ArrayList <String> lists = new ArrayList <String> ();

Lists. add (new Tangseng (). getInstance (). toString ());
Lists. add (new Tangseng (). getInstance (). toString ());

ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, lists );

ListView. setAdapter (adapter );

}
} Display result:


We can see from the figure that no matter how many new Tang Miao classes I have, the memory address is the same, which is the feature of the singleton mode, in general, we will achieve the class static of the singleton mode. What does static mean? Static methods are converted into static methods. In JAVA, static methods can be called directly without new ones. Next, let's refactor:


[Java] public class Tangseng {
 
Private static Tangseng tangseng;
 
Private Tangseng (){
// TODO Auto-generated constructor stub
}
 
Public static Tangseng getInstance (){
 
If (tangseng = null ){
Tangseng = new Tangseng ();
}
 
Return tangseng;
 
}
 
}
Public class Tangseng {

Private static Tangseng tangseng;

Private Tangseng (){
// TODO Auto-generated constructor stub
}

Public static Tangseng getInstance (){

If (tangseng = null ){
Tangseng = new Tangseng ();
}

Return tangseng;

}

} Actually, it hasn't changed much. I just added a static name before the getInstance method. In this way, the static method is implemented. What are the advantages of static methods? In Android, there are strict rules on the lifecycle of each object. For example, if the current Activity is overwritten by another Activity, the previous Activity will be recycled, but if this method implements static, it will not be recycled, so we can improve the code running efficiency. Next, let's change the call method:


[Java] public class XiyoujiActivity extends Activity {
 
Private ListView listView;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
ListView = (ListView) findViewById (R. id. listView1 );
ArrayList <String> lists = new ArrayList <String> ();
 
Lists. add (Tangseng. getInstance (). toString ());
Lists. add (Tangseng. getInstance (). toString ());
 
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, lists );
 
ListView. setAdapter (adapter );
 
}
}
Public class XiyoujiActivity extends Activity {

Private ListView listView;

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

ListView = (ListView) findViewById (R. id. listView1 );
ArrayList <String> lists = new ArrayList <String> ();

Lists. add (Tangseng. getInstance (). toString ());
Lists. add (Tangseng. getInstance (). toString ());

ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, lists );

ListView. setAdapter (adapter );

}
} Here, we don't need to use the new object and call it directly using the class name. The running result should be the same, and the two memory addresses are the same. This chapter is complete. Thank you for your attention.


From the column kangkangz4
From

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.