Public abstract class carmodel {
// This parameter is the execution sequence of each basic method.
Private arraylist <string> sequence = new arraylist <string> ();
/*
* The model starts to run.
*/
Protected abstract void start ();
// If it can be launched, it must be able to stop. That is the true skill.
Protected abstract void stop ();
// The speaker will sound like Didi or beeps.
Protected abstract void alarm ();
// The engine will boom. If it doesn't ring, it's fake.
Protected abstract void engineboom ();
// The model should be run, regardless of whether it is pushed by people or driven by electricity.
Final public void run (){
// Execute the code first when the operator is in the loop.
For (INT I = 0; I <this. sequence. Size (); I ++ ){
String actionname = This. sequence. Get (I );
If (actionname. inclusignorecase ("START") {// if it is a start keyword,
This. Start (); // turn on the car
} Else if (actionname. inclusignorecase ("stop") {// if it is a stop keyword
This. Stop (); // stop a car
} Else if (actionname. inclusignorecase ("alarm") {// If the keyword is alarm
This. Alarm (); // The Speaker starts calling.
} Else if (actionname. inclusignorecase ("engine boom") {// if it is the engine boom keyword
This. engineboom (); // the engine starts to roar
}
}
}
// Pass the passed value to the class
Final public void setsequence (arraylist <string> sequence ){
This. Sequence = sequence;
}
}
Public class benzmodel extends carmodel {
@ Override
Protected void alarm (){
System. Out. println ("the sound of the Benz horn is like this ...");
}
@ Override
Protected void engineboom (){
System. Out. println ("the car's engine room sounds ...");
}
@ Override
Protected void start (){
System. Out. println ("This is the way a Mercedes-Benz runs ...");
}
@ Override
Protected void stop (){
System. Out. println ("the Mercedes-Benz should stop like this ...");
}
}
Public class bmwmodel extends carmodel {
@ Override
Protected void alarm (){
System. Out. println ("the sound of the BMW horn is like this ...");
}
@ Override
Protected void engineboom (){
System. Out. println ("the sound of the BMW's engine room ...");
}
@ Override
Protected void start (){
System. Out. println ("This Is How bmw runs ...");
}
@ Override
Protected void stop (){
System. Out. println ("the BMW should stop like this ...");
}
}
Public class client {
Public static void main (string [] ARGs ){
/*
* The customer told niucha that I wanted such a model, and then the company told me the boss.
* We need such a model, such an order, and then I will make it.
*/
Benzmodel Benz = new benzmodel ();
// Stores the Run sequence.
Arraylist <string> sequence = new arraylist <string> ();
Sequence. Add ("engine boom"); // The customer requires that the engine be started before running.
Sequence. Add ("START"); // start up
Sequence. Add ("stop"); // stop when a part is opened.
// We give this order to the Mercedes-Benz
Benz. setsequence (sequence );
Benz. Run ();
}
}
Design Mode-the builder mode gives users freedom