4. The builder mode of the hook method is introduced.
In addition to building a complex product object, the builder model is gradually built. It is also possible to control the product creation process more finely through the Director class. For example, add a special method called the Hook Method (Hookmethod) to control whether a call to a buildpartx (), which is to infer whether a part of the product needs to be built. The return type of the hook method is usually a Boolean type, the method name is generally isxxx (), and the hook method is defined in the abstract builder class. Provides the default implementation of the Hook method in the abstract builder class. The Detailed builder class assumes no need to build a part. The builder class overrides the hook method of the abstract builder class.
Storm Video Player is a detailed product, implementation code and C + + design mode builder mode (a) blog, this is no longer present. The abstract player pattern class defines a series of hook methods and provides a default implementation. Used to infer whether a corresponding part needs to be created.
Assume that the detailed player mode does not require a part. The detailed player mode overrides the corresponding Hook method.
Playback mode. h header file code such as the following:
#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;}} Manufacturing Play form virtual void Buildwindow () = 0;//Manufacturing Play menu virtual void buildmenu () = 0;//manufacturing playlist virtual void buildplaylist () = 0;//Manufacturing Broadcast Put progress bar virtual void buildcontrolbar () = 0;//Manufacturing Favorites list virtual void buildcollectlist () = 0;//Get product (player) player * Getplayer () { return m_pplayer;} Whether to build the play form (Hook method) virtual bool Isbuildwindow () {return true;} Whether to build the Play menu (Hook method) virtual bool Isbuildmenu () {return true;} Whether to build playlist (Hook method) virtual bool Isbuildplaylist () {return true;} Whether to build the playback progress bar (Hook method) virtual bool Isbuildcontrolbar () {return true;} Whether to build a 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 ();//Full play mode does not need to build the Favorites list 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 playback menu bool Isbuildmenu () {return false;} Compact playback mode does not need to build playlist bool Isbuildplaylist () {return false;} Compact playback mode does not need to build a collection 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 playback menu bool Isbuildmenu () {return false;} Memory playback mode does not need to build playlist bool Isbuildplaylist () {return false;}}; #endif
the player mode CPP implementation code is as follows:
#include "PlayPattern.h"//Manufacturing Play form void Fullpattern::buildwindow () {M_pplayer->setwindow ("Main Interface form"); Manufacturing Playback menu void Fullpattern::buildmenu () {M_pplayer->setmenu ("main Menu");} Manufacturing playlist void Fullpattern::buildplaylist () {m_pplayer->setplaylist ("playlist"); Manufacturing playback progress bar void Fullpattern::buildcontrolbar () {M_pplayer->setcontrolbar ("progress Bar");} Manufacturing Collection list void Fullpattern::buildcollectlist () {m_pplayer->setcollectlist ("");} Compact 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 ("Favorites list");}
In the Storm AV player Contructmanage, a series of hook methods are called to infer whether the corresponding parts need to be created in different playback modes. Storm video player conductor class. h header file implementations such as the following:
#ifndef _contruct_manage_h_#define _contruct_manage_h_#include "PlayPattern.h" #include "Player.h"//Build Manager class contructmanage{private://Detailed builder Playpattern * m_pplaypattern;public://Design Player mode (that is, set detail builder) void Setplaypattern ( Playpattern * pplaypattern);//package construction process, call hook method, infer whether the corresponding parts need to build player * Construct ();}; #endif
Storm AV player Commander Class CPP file implementations such as the following:
#include " ContructManage.h "//design Player mode (that is, set detail builder) void Contructmanage::setplaypattern (Playpattern * pplaypattern) {m_ Pplaypattern = Pplaypattern;} Encapsulates the construction process, invoking a series of hook methods. Infer if the corresponding part needs to build player * CONTRUCTMANAGE::CONSTRUCT () {bool Bretval = true;//Depending on the need to build the playback form bretval = m_pplaypattern-> Isbuildwindow (); if (true = = Bretval) {M_pplaypattern->buildwindow ();} Depending on the need to build the playback menu Bretval = M_pplaypattern->isbuildmenu (); if (true = = Bretval) {m_pplaypattern->buildmenu ();} The playlist bretval = M_pplaypattern->isbuildplaylist () is required, if (true = = Bretval) {m_pplaypattern->buildplaylist ();} Depending on the need to build the playback progress bar Bretval = M_pplaypattern->isbuildcontrolbar (); if (true = = Bretval) {m_pplaypattern-> Buildcontrolbar ();} Build a collection List Bretval = M_pplaypattern->isbuildcollectlist () if required (true = = Bretval) {m_pplaypattern-> Buildcollectlist ();} Returns the built-in player player * Pplayer = M_pplaypattern->getplayer (); return pplayer;}
the 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 ContructMa Nage (); Player * Pplayer = null;/*********************** Full playback mode ************************/playpattern * Pfullpattern = new Fullpattern (); cout << "Full Play Mode:" << endl;pcontructmanage->setplaypattern (pfullpattern);pP layer = Pcontructmanage->construct ();pP layer->display ();/*********************** Compact playback mode ************************/ Playpattern * Psimplepattern = new Simplepattern () cout << "Compact playback mode:" << endl;pcontructmanage-> Setplaypattern (psimplepattern);pP layer = pcontructmanage->construct ();pP layer->display ();/**************** Memory Playback Mode ************************/playpattern * Pmemorypattern = new Memorypattern (); cout << "Memory playback mode:" < < Endl;pcontructmanage->setplaypattern (pmemorypattern);pP layer = Pcontructmanage->construct ();pP layer->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 the results such as the following:
through The introduction of Hook method. We are able to fine-tune the construction of complex products in the builder's mentor class, specifying not only the order in which the Buildpartx () method is run. You can also control whether you need to run a buildpartx () method.
5. Summary of builders ' patterns
The core of the builder model is how to build a complete object that consists of multiple components in one step, build different products using the same build process, and in software development, suppose we need to create complex objects and want the system to have very good flexibility and scalability to consider using builder mode.
1. Key Strengths
The main strengths of the builder model are as follows:
(1) In builder mode, the client does not have to know the details of the internal composition of the product, decoupling the product itself from the product creation process, enabling the same creation process to create different product objects. The builder pattern encapsulates the product's detailed creation process, which conforms to the package change principle.
(2) Each detailed builder is relatively independent. It has nothing to do with other detailed builders, so it is very easy to replace the detailed builder or add a new detailed builder, and the user can get different product objects using different detailed builders.
Because the leader class is programmed for the abstract builder, adding new detailed builders does not have to change the code of the original class library. System expansion is convenient. Conforms to the "open and close principle" and also conforms to "programming for abstraction rather than detailed programming principles".
(3) Be able to control the product creation process more finely. The steps to create a complex product are broken down into different methods. Make the creation process clearer and more convenient to use the program to control the creation process.
2. Main disadvantages
The main disadvantages of the builder pattern are as follows:
(1) The builder model created by the product generally has more common, its components are similar, it is assumed that the difference between the product is very large, such as a lot of components are not the same, not suitable for the use of the builder model, so its scope of use is limited.
(2) Assume that the internal changes of the product are complex. may lead to the need to define a very large number of detailed builder classes to achieve such a change, resulting in a very big system. Add system understanding difficulty and execution cost.
3. Detailed application of builder mode
(1) In the role of the game. There are demons, angels, heroes and other characters.
These roles include the same construction process (head, feet, appearance, etc.), and each game character construction method is different.
(2) When parsing the configuration file in XML format, it is necessary to parse the configuration file header information, data body, tail information and so on. The three processes of analysis can be considered as three processes of construction.
(3) The parsing of RTF document format is the same as in the case of parsing the same configuration file in XML format.
(4) Whether we use email to send mail, we need to fill in the message title, recipient, message content and other information. To be able to fill in the email title, recipient, mail content as three construction process.
(5) We are defining the socket network communication protocol. You need to define the data. Each is composed by the Baotou, the package body, the end of the package. So in the two sides of the communication. You can send and receive messages in the same format.
(6) The process of compiling, compiling, linking and so on should be experienced by using GCC compiler. Finally the ability to form a running program.
(7) When we use the beauty map, Photoshop software to beautify the image. To run a series of operations (sharpening, mirroring, etc.). Finally, a beautiful picture.
(8) in During the process of creating a dialog box, there will be a prompts each step of the creation process. Through a series of processes, finally form a dialog box. Similarly, wizards allow us to customize some of the appearance or functionality of the software during the installation of the software.
(9) in the process of customizing the Linux kernel. Can be based on the need to delete some unnecessary functional modules.
Customize a moderately functional operating system, commonly known as kernel cropping, and then migrate the cropped Linux system to embedded devices (arm, etc.).
(10) The Builder model of life: construction of bridges, construction of Three Gorges Project, Bird's Nest Water cube, and so on; 361, Anta, nine pastoral king a series of clothing industry construction. Hybrid rice, genetically modified rice and other grain construction.
The builder model of C + + design mode (iii)