Android Ap development and design mode Article 2: adapter Mode

Source: Internet
Author: User

Users who have developed Android Ap usually use the BaseAdapter adapter provided by Google for extension to fill in window controls such as ListView and GridView. Friends who have used it will think it is very simple and convenient to create a data source for the control. The BaseAdapter is an adapter. Google can use "existing content" to implement "Expected Results" in consideration of a feature of the adapter ".
 
Adapter Pattern
There are two adapter modes:
The Adapter Pattern of the class (that is, the so-called inheritance)
Object Adapter Pattern (the so-called delegate)
Scenario requirements
A given string is displayed in a certain style and printed in the text box of Android. According to this simple requirement, the two modes of the adapter are used to compile the Code respectively, the final project directory package is as follows:


Package name: com. terry. Pattern_one indicates class inheritance
 
Package name: com. terry. Pattern_two indicates the object delegate
 
Mode 1: class inheritance:
 
Banner class
 
Package com. terry. Pattern_one;

Public class Banner {

Private String string;

Public Banner (String string)
{
This. string = string;
}


Public String showWithParen (){
Return "(" + string + ")";
}

Public String showWithAster (){
Return "*" + string + "*";
}
}
 
Print Interface
 
Package com. terry. Pattern_one;

Public interface Print {

Public abstract String printWeak ();


Public abstract String printStrong ();

}
 
PrintBanner class
 
Package com. terry. Pattern_one;

Public class PrintBanner extends Banner implements Print {

Public PrintBanner (String string ){
Super (string );
// TODO Auto-generated constructor stub
}

@ Override
Public String printStrong (){
// TODO Auto-generated method stub
Return showWithAster ();
}

@ Override
Public String printWeak (){
// TODO Auto-generated method stub
Return showWithParen ();
}

}
 
Mode 1 uses Print to declare "results required". Next, use a PrintBanner class for adaptation and conversion to use "existing content" Banner and convert existing content to "expected result" Print. It can be understood that an alternating current with V (Banner) in daily life is converted to the available (Print) 12 V current through the adapter (PrintBanner.
 
 
Mode 2: Object delegation:
  
Print abstract class
 
Package com. terry. Pattern_two;

Public abstract class Print {

Public abstract String PrintWeak ();

Public abstract String PrintStrong ();
}
 
PrintBanner class
 
Package com. terry. Pattern_two;

Import com. terry. Pattern_one.Banner;

Public class PrintBanner extends Print {

Private Banner banner;

Public PrintBanner (String string ){
Banner = new Banner (string );
}

@ Override
Public String PrintStrong (){
// TODO Auto-generated method stub
Return banner. showWithAster ();
}

@ Override
Public String PrintWeak (){
// TODO Auto-generated method stub
Return banner. showWithParen ();
}

}
 
The "delegate" is literally put to someone for execution or agency. Mode 2 means to abstract the "desired result" (Print), and then let the PrintBanner extend its function to store the Banner object in the PrintBanner. When the system calls the PrintWeak of the PrintBanner, instead of doing it by yourself, we delegate the work to the showWithParen of the "existing content" Banner for execution.
 
The following functions can be used to increase the display mode on the Android interface:
AdapterPatternActivity class
 
Package com. terry. AdapterPattern;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;

Import com. terry. Pattern_one.Print;
Import com. terry. Pattern_one.PrintBanner;

Public class AdapterPatternActivity extends Activity {

Private Button one, two;
Private EditText etOne, etTwo;

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

One = (Button) findViewById (R. id. one );

Two = (Button) findViewById (R. id. two );

EtOne = (EditText) findViewById (R. id. et_one );

EtTwo = (EditText) findViewById (R. id. et_two );

One. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
Print p = new PrintBanner ("inheritance of Android Adapter class ");
EtOne. setText (p. printWeak () + "" + p. printStrong ());

}
});

Two. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Com. terry. Pattern_two.Print p = new com. terry. Pattern_two.PrintBanner (
"Delegate the Android Adapter object ");
EtTwo. setText (p. PrintWeak () + "" + p. PrintStrong ());
}
});

}
}
 
The display effect is as follows:


If you do projects frequently, you will have some classes that you think are good about. The adapter mode can change the previous classes to another package and re-establish the desired classes, this mode can help you save the time needed to establish the necessary method group, so as to reduce the burden and workload of writing programs.

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.