The builder model of C + + design mode (iii)

Source: Internet
Author: User
Tags compact

4. The builder mode of the hook method is introduced.

Builder mode In addition to the gradual construction of a complex product object, you can also use the Director class to more granular control of the product creation process, such as adding a class called Hook method (Hookmethod) Special method to control whether a call to a buildpartx (), is to determine 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. The default implementation of the Hook method is provided in the abstract builder class, and if the builder class does not need to construct a part, the builder class overrides the hook method of the abstract builder class.

Storm Video player is a specific 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 to determine if a corresponding part needs to be created. If the specific player mode does not require a part, the specific player mode overrides the corresponding Hook method.

Playback mode. h header file code is as follows:

#ifndef _play_pattern_h_#define _play_pattern_h_#include <iostream> #include <string> #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;}} Manufacturing Playback window 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 playback window (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 the Favorites 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 Playback window void Fullpattern::buildwindow () {M_pplayer->setwindow ("Main Interface Window");} 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 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 the Storm video player Contructmanage, a series of hook methods are called to determine if the corresponding parts need to be created in different playback modes. Storm Audio Player the command class. h header file is implemented as follows:

#ifndef _contruct_manage_h_#define _contruct_manage_h_#include "PlayPattern.h" #include "Player.h"//Build Manager class contructmanage{private://Concrete Builder Playpattern * m_pplaypattern;public://Design Player mode (that is, set specific builders) void Setplaypattern ( Playpattern * pplaypattern);//package construction process, call hook method, determine whether the corresponding components need to build player * Construct ();}; #endif
The Storm video player Commander Class CPP file is implemented as follows:
 #include " ContructManage.h "//design Player mode (that is, set concrete builder) void Contructmanage::setplaypattern (Playpattern * pplaypattern) {m_ Pplaypattern = Pplaypattern;} Package construction process, call a series of hooks method, determine whether the corresponding parts need to build player * CONTRUCTMANAGE::CONSTRUCT () {bool Bretval = true;//Build the playback window as needed bretval = M_ Pplaypattern->isbuildwindow (); if (true = = Bretval) {M_pplaypattern->buildwindow ();} Build the 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 list of favorites as needed bretval = M_pplaypattern->isbuildcollectlist (); if (true = = Bretval) {m_pplaypattern-> Buildcollectlist ();} Returns the built-in player player * Pplayer = M_pplaypattern->getplayer (); return pplayer;} 
The test program implementation code is as follows:

#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;}
compiled and executed with the following results:


through the introduction of hooks, we can fine-tune the construction of complex products in the builder's mentor class, specifying not only the order of execution of the Buildpartx () method, but also whether a buildpartx () method needs to be executed.


5. Summary of builders ' patterns

The core of the builder model is how to build a complete object with multiple components in one step, build different products using the same build process, and in software development, if we need to create complex objects and want the system to be flexible and extensible, consider using builder mode.

1. Key Benefits

The main advantages 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, allowing the same creation process to create different product objects. The builder pattern encapsulates the product-specific creation process, which conforms to the "packaging change principle".

(2) Each concrete builder is relatively independent and unrelated to other concrete builders, so it is easy to replace concrete builders or add new concrete builders, and users can obtain different product objects using different concrete builders. Since the conductor class is programmed for abstract builders, adding new concrete builders does not need to modify the code of the original class library, the system expands conveniently, conforms to the "open and close principle", and also conforms to "programming for abstraction rather than specific programming principles".

(3) The product creation process can be controlled more finely. The creation of complex products is broken down into different methods, making the creation process clearer and easier to use to control the creation process.

2. Main disadvantages

The main drawbacks of the builder model are as follows:

(1) The product created by the builder model generally has a lot of common ground, its components are similar, if the difference between the product is very large, for example, many components are not the same, not suitable for the use of the builder model, so its scope of use is limited.

(2) If the internal variation of the product is complex, it may lead to the need to define many concrete builder classes to achieve this change, resulting in a large system, increasing the difficulty of understanding the system and operating costs.

3. Concrete application of the builder model

(1) In the role of the game, there are demons, angels, heroes and other characters. These roles contain the same construction process (head, foot, appearance, etc.), and each game character is built differently.

(2) When parsing a 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 parsing can be considered as three processes of construction.

(3) parsing the RTF document format also exists in the same situation as parsing the XML-formatted configuration file.

(4) Whether we use email to send mail, we need to fill in the message title, recipient, message content and other information. You can take the message title, the recipient, the message content as three construction process.

(5) When we define the socket network communication protocol, we need to define the data, each of which consists of Baotou, the package body and the end of the packet. In this way, both sides of the communication can send and receive information in the same format.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.