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

Source: Internet
Author: User
Tags compact

3. Omit the builder model of the conductor Director

Conductor Class Director play critical data in the builder model. The simple Director class is used to guide detailed builders on how to build a product, which calls Builder 's buildpartx () in a certain order. method. Controls the order of calls and returns a complete product object to the client.

DIRECOTR is programmed for abstract builders, assuming that different builders are needed. Only the builders should be introduced into the command class. No need to change the previous code.

in some cases, in order to simplify the system structure, it is possible to omit the conductor director, the conductor no longer directs the product creation process . Instead , the construct () method is provided in Builder to progressively build complex product objects .

Storm Video Player is a detailed product, the implementation code and the previous blog, this is no longer present.

Change is the builder class. Playback mode. h header file implementation code such as the following:

< Span style= "line-height:26px" >

#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 Collection list virtual void buildcollectlist () = 0;//started construction. Package 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 ();};/ /compact 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
Mentor class no longer directs the creation process of the Storm AV product object. Instead , the abstract player mode Startconstruct method is used to encapsulate the creation process of the player product.
Playback mode CPP file code such as the following:

#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 Favorites list, full play mode no Favorites list, content set to empty 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");}
The full playback mode does not require a collection list part to be built, so the contents of the Favorites list are set to empty. Other case types, set the content of components that do not need to be built to be empty. The test program implementation code such as the following:

#include <iostream> #include "PlayPattern.h" #include "Player.h" using namespace Std;int Main () {Player * Pplayer = null;/*********************** Full playback mode ************************/playpattern * Pfullpattern = new FullPattern (); cout < < "full playback mode:" << Endl;pplayer = pfullpattern->startconstruct ();pP layer->display (); /*********************** Compact Playback Mode ************************/simplepattern * Psimplepattern = new SimplePattern (); cout  << "Compact playback mode:" << Endl;pplayer = psimplepattern->startconstruct ();pP layer->display (); /*********************** Memory Playback Mode ************************/memorypattern * Pmemorypattern = new MemoryPattern (); cout << "Memory playback mode:" << Endl;pplayer = pmemorypattern->startconstruct ();pP layer->display ();/************* Destroy Operation ****************************/cout << endl;delete pfullpattern;pfullpattern = Null;delete Psimplepattern;psimplepattern = Null;delete Pmemorypattern;pmemorypattern = Null;return 0;}
compile and run the results such as the following:


At this point, the Startconstruct () method defines the order of the other Buildpartx () method calls. Provides a process template for the operation of other methods, similar to the template method pattern we are going to learn later. The omission of the Director class does not affect the flexibility and extensibility of the system, but also simplifies the structure of the system at the same time, but aggravates the responsibilities of the abstract builder class. Assuming that the startconstruct() method is more complex and more components are being built, it is recommended that the startconstruct() method be encapsulated separately in the director. This is more in line with the "single Responsibility Principle"


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

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.