Hello, everyone. Meet again. I'm Jun Quan.
4. Introduce the builder pattern of hook method
In addition to gradually building a complex product object, the builder model. You can also control the product creation process more finely through the Director class. For example, add a special method called hook method to control whether to call a buildPartX(), that is, infer whether a part in the product needs to be built. The return type of hook method is usually boolean type, and the method name is generally isXXX(). The hook method is defined in the abstract builder class. Provide a default implementation of the hook method in the abstract builder class. The detailed builder class assumes that it is not necessary to build a component. The builder class overrides the hook method of the abstract builder class.
Storm video player is a detailed product, implementation code and C++ Design The builder of mode (I) like blog, it will not be presented here. The abstract player pattern class defines a series of hook methods and provides a default implementation. Used to infer whether the corresponding assembly needs to be created.
Assume that a component is not required for detailed player mode. The detailed player mode overrides the corresponding hook method.
Play mode h header file code, for example:
#ifndef _PLAY_PATTERN_H_#define _PLAY_PATTERN_H_#include <iostream>#include <string>#include "Player.h"using namespace std;//Abstract playback mode class PlayPattern{protected: // Detailed product (player) Player * m_pPlayer;public: PlayPattern() { m_pPlayer = new Player(); } ~ PlayPattern() { if( NULL != m_pPlayer ) { delete m_pPlayer; m_pPlayer = NULL; } } // Create playback form virtual void BuildWindow() = 0; // Create playback menu virtual void BuildMenu() = 0; // Make playlist virtual void BuildPlayList() = 0; // Manufacturing playback progress bar virtual void BuildControlBar() = 0; // Manufacturing collection list virtual void BuildCollectList() = 0; // Get product (player) Player * GetPlayer() { return m_pPlayer; } // Whether to build playback form (hook method) virtual bool IsBuildWindow() { return true; } // Whether to build play menu (hook method) virtual bool IsBuildMenu() { return true; } // Whether to build playlist (hook method) virtual bool IsBuildPlayList() { return true; } // Whether to build playback progress bar (hook method) virtual bool IsBuildControlBar() { return true; } // Whether to build collection list (hook method) virtual bool IsBuildCollectList() { return true; }};// Full playback mode class fullpattern: public playpattern {public: void BuildWindow(); void BuildMenu(); void BuildPlayList(); void BuildControlBar(); void BuildCollectList(); // There is no need to build a favorite list for full playback mode bool IsBuildCollectList() { return false; }};// Compact playback mode class simplepattern: public playpattern {public: void BuildWindow(); void BuildMenu(); void BuildPlayList(); void BuildControlBar(); void BuildCollectList(); // Compact playback mode does not need to build a playback menu bool IsBuildMenu() { return false; } // Compact playback mode does not need to build playlists bool IsBuildPlayList() { return false; } // Compact play mode does not need to build a favorite list bool IsBuildCollectList() { return false; }};// Memory playback mode class memorypattern: public playpattern {public: void BuildWindow(); void BuildMenu(); void BuildPlayList(); void BuildControlBar(); void BuildCollectList(); // Memory playback mode does not need to build a playback menu bool IsBuildMenu() { return false; } // Memory playback mode does not need to build playlists bool IsBuildPlayList() { return false; }};# endif
The implementation code of player mode Cpp is as follows:
#include "PlayPattern.h" / / create play form void FullPattern::BuildWindow(){ m_ Pplayer - > SetWindow ("main interface form");}// Create playback menu void FullPattern::BuildMenu(){ m_ Pplayer - > SetMenu ("main menu");}// Create playlist void FullPattern::BuildPlayList(){ m_ Pplayer - > setplaylist ("playlist");}// Create playback progress bar void FullPattern::BuildControlBar(){ m_ Pplayer - > setcontrolbar ("progress bar");}// Manufacturing collection list void FullPattern::BuildCollectList(){ m_ pPlayer->SetCollectList(" ");}//////////////// Thin mode / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / void SimplePattern::BuildWindow(){ m_ Pplayer - > SetWindow ("main interface form");} 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 form");} 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 ("collection list");}
In the storm video player director ContructManage, a series of hook methods have been invoked to infer whether different parts need to be created in different playback modes. Storm video player director class h header file implementation, for example:
#ifndef _CONTRUCT_MANAGE_H_#define _CONTRUCT_MANAGE_H_#include "PlayPattern.h"#include "Player.h"//Construction manager class constructmanage {private: // Detailed builder PlayPattern * m_pPlayPattern;public: // Design player mode (i.e. set detailed builder) void SetPlayPattern(PlayPattern * pPlayPattern); // Encapsulate the construction process, call the hook method, and infer whether the corresponding parts need to be constructed Player * Construct();};#endif
The implementation of storm video player commander class Cpp file is as follows:
#include "ContructManage.h" //Design player mode (i.e. set detailed builder) void ContructManage::SetPlayPattern(PlayPattern * pPlayPattern) { m_pPlayPattern = pPlayPattern; } //Encapsulate the construction process and call a series of hook methods. Infer whether the corresponding components need to be built Player * ContructManage::Construct() { bool bRetVal = true; //Build a playback window as needed bRetVal = m_pPlayPattern->IsBuildWindow(); if( true == bRetVal ) { m_pPlayPattern->BuildWindow(); } //Build play menu as needed bRetVal = m_pPlayPattern->IsBuildMenu(); if( true == bRetVal ) { m_pPlayPattern->BuildMenu(); } //Build playlists as needed bRetVal = m_pPlayPattern->IsBuildPlayList(); if( true == bRetVal ) { m_pPlayPattern->BuildPlayList(); } //Build the playback progress bar as needed bRetVal = m_pPlayPattern->IsBuildControlBar(); if( true == bRetVal ) { m_pPlayPattern->BuildControlBar(); } //Build a collection list as needed bRetVal = m_pPlayPattern->IsBuildCollectList(); if( true == bRetVal ) { m_pPlayPattern->BuildCollectList(); } //Return to the built player Player * pPlayer = m_pPlayPattern->GetPlayer(); return pPlayer; }
Test program implementation code, such as the following:
#include <iostream>#include "ContructManage.h"#include "PlayPattern.h"#include "Player.h"using namespace std;int main() {/ *********************** create build manager**********************/ ContructManage * pContructManage = new ContructManage(); Player * pPlayer = NULL; /*********************** Full playback mode************************/ PlayPattern * pFullPattern = new FullPattern(); Cout < < full playback mode: < < endl; pContructManage->SetPlayPattern(pFullPattern); pPlayer = pContructManage->Construct(); pPlayer->Display(); /*********************** Compact playback mode************************/ PlayPattern * pSimplePattern = new SimplePattern(); Cout < < compact playback mode: < < endl; pContructManage->SetPlayPattern(pSimplePattern); pPlayer = pContructManage->Construct(); pPlayer->Display(); /*********************** Memory playback mode************************/ PlayPattern * pMemoryPattern = new MemoryPattern(); Cout < < memory playback mode: < < endl; pContructManage->SetPlayPattern(pMemoryPattern); pPlayer = pContructManage->Construct(); pPlayer->Display(); /*********************** Destroy operation****************************/ cout << endl; delete pFullPattern; pFullPattern = NULL; delete pSimplePattern; pSimplePattern = NULL; delete pMemoryPattern; pMemoryPattern = NULL; delete pContructManage; pContructManage = NULL; return 0;}
Compile and run, and the results are as follows:

By introducing the hook method. We can fine control the construction of complex products in the builder instructor class, not only specifying the running order of buildPartX() method. You can also control whether you need to run a buildPartX() method.
5. Builder mode summary
The core of builder mode is how to build a complete object including multiple components step by step, and use the same construction process to build different products. In software development, assuming that we need to create complex objects and hope that the system has very good flexibility and scalability, we can consider using builder mode.
1. Main strengths
The main advantages of the builder model are as follows:
(1) In the builder mode, the client does not need to know the details of the internal composition of the product, decouples the product itself from the product creation process, so that the same creation process can create different product objects. The builder mode encapsulates the detailed creation process of the product and conforms to the "principle of packaging change".
(2) Each detailed builder is relatively independent. It has nothing to do with other detailed builders, so it is very convenient to replace or add new detailed builders. Users can get different product objects by using different detailed builders.
Because the commander class is programmed for the abstract builder, adding a new detailed builder does not need to change the code of the original class library. The system is easy to expand. It conforms to the "opening and closing principle" and "programming for abstraction rather than detailed programming principle".
(3) It can more finely control the product creation process. The creation steps of complex products are decomposed into different methods. It makes the creation process clearer and easier to use programs to control the creation process.
2. Main disadvantages
The main disadvantages of the builder model are as follows:
(1) The products created by the builder mode generally have more in common, and their components are similar. Assuming that the differences between products are very large, for example, many components are different, so it is not suitable to use the builder mode, so its scope of use is limited.
(2) It is assumed that the internal changes of the product are complex. It may lead to the need to define many detailed builder classes to implement such changes, resulting in a very large system. Add the difficulty of understanding and implementation cost of the system.
3. Detailed application of builder mode
(1) In game characters. There are demons, angels, heroes and other characters.
These characters include the same construction process (head, feet, appearance, etc.), and each game character has different construction methods.
(2) When parsing the configuration file in XML format, it is necessary to parse the header information, data body, tail information, etc. of the configuration file. The three processes of analysis can be regarded as the three processes of construction.
(3) Parsing the Rtf document format is the same as parsing the configuration file in XML format.
(4) If we use Email to send mail, we need to fill in the mail title, recipient, mail content and other information. The ability to fill in the Email title, recipient and Email content can be regarded as three construction processes.
(5) When we define the Socket network communication protocol. Data frames need to be defined. Each frame consists of Baotou, BaoTi and Baowei. In this way, both sides of the communication. Can send and receive information in the same format.
(6) Using Gcc compiler needs to go through the process of compilation, assembly, link and so on. Finally, we can form a runnable program.
(7) When we use Meitu and Photoshop software to beautify images. You have to run a series of operations (sharpening, mirroring, etc.). Finally there was a gorgeous picture.
(8) In the process of creating the dialog box program, a wizard will prompt each step of the creation process. After a series of processes, a dialog box is finally formed. Similarly, in the process of installing the software, a wizard will appear to let us customize some appearance or functions of the software.
(9) In the process of customizing the Linux kernel. It can delete some unnecessary functional modules according to needs.
Customize an operating system with moderate functions, commonly known as kernel tailoring, and then transplant the tailoring Linux system to embedded devices (ARM, etc.).
(10) Application of builder mode in life: bridge construction, Three Gorges project construction, bird's nest water cube and a series of project construction; 361. Construction of a series of garment industries in Anta and jiumuwang. Hybrid rice, genetically modified rice and other grain construction.
Publisher: full stack programmer, stack length, please indicate the source for Reprint: https://javaforall.cn/116542.html Original link: https://javaforall.cn