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