Android [advanced tutorial] Design Mode 3 prototype mode

Source: Internet
Author: User

Two design models were introduced last time. This time we will talk about the prototype. The prototype is actually easy to understand. It is a prototype, a clone of its own implementation. In travel to the West, there are often events such as the true and false Tang monks and the true and false Sun Wukong. Here we will talk about the prototype based on this event. First of all, if you do false, you must have the characteristics of your own identity (for example: appearance, accent) These features must also be the same as the original, in order to make people really difficult. Next we will use the previous policy mode to implement the prototype. First, we will define the Wukong class:

[Java] public class Wukong implements Person, Cloneable {
 
@ Override
Public String save (){
 
Return "Big apprentice Sun Wukong to save the master ";
}
 
@ Override
Protected Object clone (){
Try {
Return super. clone ();
} Catch (CloneNotSupportedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Return null;
}
}
 
}
Public class Wukong implements Person, Cloneable {

@ Override
Public String save (){

Return "Big apprentice Sun Wukong to save the master ";
}

@ Override
Protected Object clone (){
Try {
Return super. clone ();
} Catch (CloneNotSupportedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Return null;
}
}

} Here Wukong implements its own replication, that is, cloning, and copying its own classes, so that it can be truly difficult. The next step is to call it.

[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 );

Tangseng tangseng = new Tangseng ();
Wukong wukong = new Wukong ();

ArrayList <String> lists = new ArrayList <String> ();
Lists. add (tangseng. help (wukong ));
Lists. add (tangseng. help (Person) wukong. clone ()));
 
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 );

Tangseng tangseng = new Tangseng ();
Wukong wukong = new Wukong ();

ArrayList <String> lists = new ArrayList <String> ();
Lists. add (tangseng. help (wukong ));
Lists. add (tangseng. help (Person) wukong. clone ()));

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

ListView. setAdapter (adapter );

}
} For other classes, we borrowed the class in the previous policy mode. The other classes are not changed. Here we will explain that, because the cloned class returns an object and we perform a forced conversion, return him to a specific type, so that Tang Miao can save the call when he is calling for help.

Finally,

 


From the column kangkangz4

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.