Design Mode-the builder mode gives users freedom

Source: Internet
Author: User
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

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.