Design Mode-the builder mode gives users freedom
Public abstract class CarModel {
// This parameter is the execution sequence of each basic method.
Private ArrayList Sequence = new ArrayList ();
/*
* 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 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 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 Sequence = new ArrayList ();
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 ();
}
}