Builder mode: Separates the representation of a complex object from its implementation so that it can construct a non-accessible representation under the same construction.
The above words may not be very clear, for example, I understand. Suppose that a company's software project process is fixed, need to understand requirements, requirements design, requirements review, coding, testing this five steps, but this company is a first-level supplier, after the project, some modules need to contract to two-tier suppliers, such as HMI, media two module requirements outsourcing, However, according to the project requirements, all modules (including outsourced modules) must implement the basic steps of requirements understanding, requirements design, requirements review, coding, testing.
If this is expressed in object-oriented, then we can design a project class, including requirements understanding, requirements design, requirements review, coding, testing these 5 interfaces, followed by the design of an abstract supplier class, to provide 5 implementation requirements understanding, requirements design, requirements review, coding, testing methods, Then design an HMI class and a media class, and inherit from the Supplier class, to achieve the requirements of understanding, requirements design, requirements review, coding, testing the 5 methods; finally define a company class, the company class contains a supplier class variables, in the company class to define a project method, In this method, the requirement understanding, requirement design, requirement review, coding and testing of the supplier class are implemented separately. After all, the language is a little pale, look at the UML class diagram:
Then look at the code to make it more clear:
#include <iostream>#include<string>using namespacestd;classProject//Project Class{ Public: Project (): M_requirement (""), M_detaileddes (""), M_designrev (""), M_coding (""), M_test (""){} voidRequirementunderstand (stringStr//Demand Understanding{cout<<"Project Requirementunderstand ()"<<Endl; M_requirement=str; } voidDetaileddesign (stringStr//Detailed Design{cout<<"Project detaileddesign ()"<<Endl; M_detaileddes=str; } voidDesignreview (stringStr//Design Check{cout<<"Project Designreview ()"<<Endl; M_designrev=str; } voidCodingstringStr//Coding{cout<<"Project Coding ()"<<Endl; M_designrev=str; } voidTeststringStr//Test{cout<<"Project Test ()"<<Endl; M_designrev=str; }Private: stringm_requirement; stringM_detaileddes; stringM_designrev; stringm_coding; stringm_test;};classSupplier//supplier base class, level two supplier{ Public: Virtual voidImplentrequirementunderstand () =0;//Realizing the needs understanding Virtual voidImplentdetaileddesign () =0;//Implement detailed design Virtual voidImplentdeignreview () =0;//Implement design Checks Virtual voidImplentcoding () =0;//Implementing the Encoding Virtual voidImplenttest () =0;//Implementation Testing Virtual~Supplier () {}};classSupplierhmi: PublicSupplier//HMI Suppliers{ Public: Supplierhmi () {M_pro=NewProject (); } ~Supplierhmi () {if(NULL! =M_pro) { DeleteM_pro; M_pro=NULL; } } Virtual voidImplentrequirementunderstand (void) {cout<<"Supplierhmi Implentrequirementunderstand ()"<<Endl; M_pro->requirementunderstand ("HMI Requirementunderstand"); } Virtual voidImplentdetaileddesign (void) {cout<<"Supplierhmi implentdetaileddesign ()"<<Endl; M_pro->detaileddesign ("HMI detaileddesign" ); } Virtual voidImplentdeignreview (void) {cout<<"Supplierhmi Implentdeignreview ()"<<Endl; M_pro->designreview ("HMI Designreview" ); } Virtual voidImplentcoding (void) {cout<<"supplierhmiproject implentcoding ()"<<Endl; M_pro->coding ("HMI Coding"); } Virtual voidImplenttest (void) {cout<<"Supplierhmi implenttest ()"<<Endl; M_pro->test ("HMI Test"); }Private: Project*M_pro;};classSuppliermedia: PublicSupplier//Media provider{ Public: Suppliermedia () {M_pro=NewProject (); } ~Suppliermedia () {if(NULL! =M_pro) { DeleteM_pro; M_pro=NULL; } } Virtual voidImplentrequirementunderstand (void) {cout<<"Suppliermedia Implentrequirementunderstand ()"<<Endl; M_pro->requirementunderstand ("Media Requirementunderstand"); } Virtual voidImplentdetaileddesign (void) {cout<<"Suppliermedia implentdetaileddesign ()"<<Endl; M_pro->detaileddesign ("Media detaileddesign"); } Virtual voidImplentdeignreview (void) {cout<<"Suppliermedia Implentdeignreview ()"<<Endl; M_pro->designreview ("Media Designreview"); } Virtual voidImplentcoding (void) {cout<<"Suppliermedia implentcoding ()"<<Endl; M_pro->coding ("Media Coding"); } Virtual voidImplenttest (void) {cout<<"Suppliermedia implenttest ()"<<Endl; M_pro->test ("Media Test"); }Private: Project*M_pro;};classCompany//Company, a tier one supplier{Private: Supplier*M_supplier; Public: Company (Supplier*sup): M_supplier (SUP) {}~Company () {if(NULL! =m_supplier) { DeleteM_supplier; M_supplier=NULL; } } voidDoproject ()//do this project{m_supplier-Implentrequirementunderstand (); M_supplier-implentdetaileddesign (); M_supplier-Implentdeignreview (); M_supplier-implentcoding (); M_supplier-implenttest (); }};intMainintargcChar**argv) {Supplier* Supplier =NewSupplierhmi (); Company company (supplier); Company.doproject (); return 0;}
The output is as follows:
Builder mode (c + +)