I. Overview
The Builder mode solves the following problems: When the objects to be created are complex (usually composed of many other objects ), we need to separate the process of creating a complex object from the representation (Display) of this object. The advantage of this is to build a complex object step by step, because parameters can be introduced in the construction process of each step, the presentation of objects obtained after the creation of the same step is different.
Ii. Example
The hamburger generation process of KFC and Mcdonalds is roughly the same. Assume there are four steps;
However, the taste of KFC and Mcdonalds hamburger is different, mainly in the details of each step.
Customers do not care about the specific production steps. In fact, they do not care about the hamburger store because these steps have been the same for hundreds of years. The difference is only in detail. For example, how much salt is put and how much chilies are put.
The structure is as follows:
Builder: the most basic production steps
KFCBuilder: specific production steps for KFC
MCDBuilder: The specific production steps of Mcdonalds
Director: Used as the conductor to control the construction process.
[Cpp] // /////////////////////////////////////
// Builder
Class Builder
{
Public:
Virtual ~ Builder ()
{
}
Virtual void BuildSetp1 () = 0;
Virtual void BuildSetp2 () = 0;
Virtual void BuildSetp3 () = 0;
Virtual void BuildSetp4 () = 0;
};
// Specific KFC Builder
Class KFCBuilder: public Builder
{
Public:
Void BuildSetp1 ()
{
Cout <"KFC Step1:" <endl;
}
Void BuildSetp2 ()
{
Cout <"KFC Step2:" <endl;
}
Void BuildSetp3 ()
{
Cout <"KFC Step3:" <endl;
}
Void BuildSetp4 ()
{
Cout <"KFC Step4:" <endl;
}
};
// Specific builder of Mcdonalds
Class MCDBuilder: public Builder
{
Public:
Void BuildSetp1 ()
{
Cout <"Mcdonalds Step1:" <endl;
}
Void BuildSetp2 ()
{
Cout <"Mcdonalds Step2:" <endl;
}
Void BuildSetp3 ()
{
Cout <"Mcdonalds Step3:" <endl;
}
Void BuildSetp4 ()
{
Cout <"Mcdonalds Step4:" <endl;
}
};
//////////////////////////////////////// //////////////////////////////////
// Conductor
Class Director
{
Private:
Builder * m_pBuilder;
Public:
Director (Builder * builder)
{
M_pBuilder = builder;
}
Void Create ()
{
M_pBuilder-> BuildSetp1 ();
M_pBuilder-> BuildSetp2 ();
M_pBuilder-> BuildSetp3 ();
M_pBuilder-> BuildSetp4 ();
}
};
//////////////////////////////////////// //////////////////////////////////
// Test code
Int main (int argc, char * argv [])
{
KFCBuilder kfc; // you want to eat KFC
Director dire( & kfc );
Director. Create ();
Return 0;
}
//////////////////////////////////////// //////////////////////////////////
// Builder
Class Builder
{
Public:
Virtual ~ Builder ()
{
}
Virtual void BuildSetp1 () = 0;
Virtual void BuildSetp2 () = 0;
Virtual void BuildSetp3 () = 0;
Virtual void BuildSetp4 () = 0;
};
// Specific KFC Builder
Class KFCBuilder: public Builder
{
Public:
Void BuildSetp1 ()
{
Cout <"KFC Step1:" <endl;
}
Void BuildSetp2 ()
{
Cout <"KFC Step2:" <endl;
}
Void BuildSetp3 ()
{
Cout <"KFC Step3:" <endl;
}
Void BuildSetp4 ()
{
Cout <"KFC Step4:" <endl;
}
};
// Specific builder of Mcdonalds
Class MCDBuilder: public Builder
{
Public:
Void BuildSetp1 ()
{
Cout <"Mcdonalds Step1:" <endl;
}
Void BuildSetp2 ()
{
Cout <"Mcdonalds Step2:" <endl;
}
Void BuildSetp3 ()
{
Cout <"Mcdonalds Step3:" <endl;
}
Void BuildSetp4 ()
{
Cout <"Mcdonalds Step4:" <endl;
}
};
//////////////////////////////////////// //////////////////////////////////
// Conductor
Class Director
{
Private:
Builder * m_pBuilder;
Public:
Director (Builder * builder)
{
M_pBuilder = builder;
}
Void Create ()
{
M_pBuilder-> BuildSetp1 ();
M_pBuilder-> BuildSetp2 ();
M_pBuilder-> BuildSetp3 ();
M_pBuilder-> BuildSetp4 ();
}
};
//////////////////////////////////////// //////////////////////////////////
// Test code
Int main (int argc, char * argv [])
{
KFCBuilder kfc; // you want to eat KFC
Director dire( & kfc );
Director. Create ();
Return 0;
}
Iii. Description
1. The Builder mode is usually stable in the construction sequence.
2. the conductor is used to isolate the association between users and the specific construction process.
3. The advantage is that the client does not need to know the specific builder method, but does not need to forget that a specific step is not written. These steps are called by Director.
Author lwbeyond