Builder mode of C ++ design mode (2)
3. The Builder mode of the conductor Director is omitted.
The conductor ctor plays an important role in the Builder mode. A simple Director class is used to guide specific builders in building products. It calls the buildPartX () method of Builder in a certain order, controls the order of calls and returns a complete product object to the client. Direcotr is designed for abstract builders. If you need different builders, you only need to pass the builders into the conductor class without modifying the previous code.
In some cases, the conductor ctor can be omitted to simplify the system structure, and the conductor no longer guides the product creation process. Instead, the construct () method is provided in Builder to gradually build complex product objects.
Apsaravideo player is a specific product. Its implementation code is the same as that of the previous blog, so it will not be displayed here. The builder class is changed. The implementation code of the. h header file in the playback mode is as follows:
# Ifndef _ PLAY_PATTERN_H _ # define _ PLAY_PATTERN_H _ # include
# Include
# Include "Player. h "using namespace std; // abstract playback mode class PlayPattern {protected: // specific product (Player) Player * m_pPlayer; public: PlayPattern () {m_pPlayer = new Player ();}~ PlayPattern () {if (NULL! = M_pPlayer) {delete m_pPlayer; m_pPlayer = NULL; }}// create a playback window virtual void BuildWindow () = 0; // create a playback menu virtual void BuildMenu () = 0; // manufacturing playlist virtual void BuildPlayList () = 0; // manufacturing playlist virtual void BuildControlBar () = 0; // manufacturing favorite list virtual void BuildCollectList () = 0; // start construction and encapsulate the construction process Player * StartConstruct () {BuildWindow (); BuildMenu (); BuildPlayList (); BuildControlBar (); BuildCollectList (); return m_pPlayer ;}}; // full playback mode class FullPattern: public PlayPattern {public: void BuildWindow (); void BuildMenu (); void BuildPlayList (); void BuildControlBar (); void BuildCollectList ();}; // simplified playback mode class SimplePattern: public PlayPattern {public: void BuildWindow (); void BuildMenu (); void BuildPlayList (); void BuildControlBar (); void BuildCollectList ();}; // memory playback mode class MemoryPattern: public PlayPattern {public: void BuildWindow (); void BuildMenu (); void BuildPlayList (); void BuildControlBar (); void BuildCollectList ();}; # endif
The instructor class no longer guides the creation process of storm audio and video product objects, but encapsulates the creation process of the player product by using the abstract player mode StartConstruct.
The code for the Cpp file in playback mode is as follows:
# Include "PlayPattern. h "// manufacturing the playback window void FullPattern: BuildWindow () {m_pPlayer-> SetWindow (" main interface window ");} // manufacturing the playback menu void FullPattern: BuildMenu () {m_pPlayer-> SetMenu ("Main Menu");} // creates a playlist void FullPattern: BuildPlayList () {m_pPlayer-> SetPlayList ("playlist ");} // create a playback progress bar void FullPattern: BuildControlBar () {m_pPlayer-> SetControlBar ("progress bar");} // create a favorite list. The complete playback mode has no favorite list, void FullPattern: BuildCollectList () {m_pPlayer-> SetCollectList ("");} /// // simplified mode ////////////////////// ///// // void SimplePattern:: BuildWindow () {m_pPlayer-> SetWindow ("main interface window");} void SimplePattern: BuildMenu () {m_pPlayer-> SetMenu ("");} void SimplePattern :: buildPlayList () {m_pPlayer-> SetPlayList ("");} void SimplePattern: BuildControlBar () {m_pPlayer-> SetControlBar ("progress bar");} void SimplePattern: BuildCollectList () {m_pPlayer-> SetCollectList ("");} //// // memory mode ///////////////////// ///////// void MemoryPattern:: BuildWindow () {m_pPlayer-> SetWindow ("main interface window");} void MemoryPattern: BuildMenu () {m_pPlayer-> SetMenu ("");} void MemoryPattern :: buildPlayList () {m_pPlayer-> SetPlayList ("");} void MemoryPattern: BuildControlBar () {m_pPlayer-> SetControlBar ("progress bar");} void MemoryPattern: BuildCollectList () {m_pPlayer-> SetCollectList ("Favorites list ");}In full playback mode, you do not need to create a favorite list part. Therefore, set the content of the favorite list to null. In other cases, set the content of the parts that do not need to be created to null. The implementation code of the test program is as follows:
# Include
# Include "PlayPattern. h "# include" Player. h "using namespace std; int main () {Player * pPlayer = NULL; ************* * *********/PlayPattern * pFullPattern = new FullPattern (); cout <"Full playback mode:" <endl; pPlayer = pFullPattern-> StartConstruct (); pPlayer-> Display (); /*********************** simplified playback mode ************* * *********/SimplePattern * pSimplePattern = new SimplePattern (); cout <"simplified playback mode:" <endl; pPlayer = pSimplePattern-> StartConstruct (); pPlayer-> Display (); /*********************** memory playback mode ************* * *********/MemoryPattern * pMemoryPattern = new MemoryPattern (); cout <"memory playback mode:" <endl; pPlayer = pMemoryPattern-> StartConstruct (); pPlayer-> Display (); ************** * *************/cout <endl; delete pFullPattern; pFullPattern = NULL; delete pSimplePattern; pSimplePattern = NULL; delete pMemoryPattern; pMemoryPattern = NULL; return 0 ;}
Compile and execute the command. The result is as follows:
In this case, the StartConstruct () method defines the order of calls of other buildPartX () methods and provides a Process Template for execution of other methods, this is very similar to the template method model we will learn later. The omission of Director class does not affect the flexibility and scalability of the system. It also simplifies the system structure, but increases the responsibilities of the abstract builder class. If the StartConstruct () method is more complex, it is recommended that the StartConstruct () method be separately encapsulated in ctor if there are many components of the product to be built. This is more in line with the "single responsibility principle"