Design Pattern Generation generator Pattern

Source: Internet
Author: User

The builder mode is mainly used to create complex objects, focusing on internal building of complex objects. By separating the build process from the representation, the same build process can produce different external representations. The builder mode consists of a guide and builder. The guide is responsible for the construction process, and the builder is responsible for the external representation of the production object. The guide contains a generator. To replace the object representation, you only need to replace the generator.

In the previous article, abstract factory focuses more on the creation of multi-mask series. Now let's change the focus. Now we provide information about all the images on a mask. This information can be stored in formatted files, such as XML files. For convenience, without affecting understanding, we use a simple text file. A string represents a graph on the mask. For example, reading "round" indicates that there is a circle on the mask. The definition class builder acts as a guide and has a construction process structure that reads files and builds images one by one. The class maskfigure is a virtual base class that contains interfaces for building different images. The derived subclass is a generator that can generate images with different characteristics, that is, it is responsible for external representation. Builder contains different maskfigure subclass to generate images with different characteristics without modifying the construction process interface. Class interface:

 

The code is implemented as follows:

//mask.hpp#ifndef MASK_HPP#define MASK_HPPclass MaskFigure{  public:    virtual ~MaskFigure()=0;    virtual void CreateRound()=0;    virtual void CreateRec()=0;    virtual void CreateTri()=0;  protected:    MaskFigure();};class MaskAFigure:public MaskFigure {  public:    MaskAFigure();    ~MaskAFigure();    void CreateRound();    void CreateRec();    void CreateTri();};class MaskBFigure:public MaskFigure {  public:    MaskBFigure();    ~MaskBFigure();    void CreateRound();    void CreateRec();    void CreateTri();};#endif//mask.cpp#include <iostream>#include "mask.hpp"using std::cout;using std::endl;MaskFigure::MaskFigure() {}MaskFigure::~MaskFigure() {}MaskAFigure::MaskAFigure(){  cout<<"init MaskAFigure "<<endl;}MaskAFigure::~MaskAFigure() {  cout<<"delete MaskAFigure"<<endl;}void MaskAFigure::CreateRound(){  cout<<"create MaskA Round"<<endl;}void MaskAFigure::CreateRec(){  cout<<"create MaskA Rec"<<endl;}void MaskAFigure::CreateTri(){  cout<<"create MaskA Tri"<<endl;}MaskBFigure::MaskBFigure(){  cout<<"init MaskBFigure "<<endl;}MaskBFigure::~MaskBFigure() {  cout<<"delete MaskBFigure"<<endl;}void MaskBFigure::CreateRound(){  cout<<"create MaskB Round"<<endl;}void MaskBFigure::CreateRec(){  cout<<"create MaskB Rec"<<endl;}void MaskBFigure::CreateTri(){  cout<<"create MaskB Tri"<<endl;}//builder.hpp#ifndef BUILDER_HPP#define BUILDER_HPP#include <iostream>#include <fstream>#include <vector>#include "mask.hpp"using std::vector;using std::cout;using std::endl;using std::ifstream;using std::string;class MaskBuilder { public:   MaskBuilder(string &, MaskFigure*);   ~MaskBuilder();   bool openFile(string &);   void BuildMask();   void setMaskFigure(MaskFigure*); private:   ifstream inf;   MaskFigure *mf;};#endif//builder.cpp#include <string>#include "builder.hpp"MaskBuilder::MaskBuilder(string &filename, MaskFigure *_mf):inf(), mf(_mf) {  openFile(filename);}MaskBuilder::~MaskBuilder(){  inf.close();}bool MaskBuilder::openFile(string& filename){  if(inf.is_open()){    inf.close();    inf.clear(std::ios_base::goodbit);  }  inf.open(filename);  if(!inf){    cout<<"open file \""<<filename<<"\" failure"<<endl;    return false;  }  return true;}void MaskBuilder::setMaskFigure(MaskFigure* _mf) {  mf = _mf;}void MaskBuilder::BuildMask(){  string ftype;  while(inf>>ftype){    if(ftype == "round"){      mf->CreateRound();    }else if(ftype == "rec"){      mf->CreateRec();    }else if(ftype == "tri"){      mf->CreateTri();    }else {      cout<<"undefine figure type: "<<ftype<<endl;    }  }}//main.cpp#include <iostream>#include "builder.hpp"using std::cout;using std::endl;int main() {  string filename("types");  MaskAFigure maf;  MaskBuilder mb(filename,&maf);  mb.BuildMask();  MaskBFigure mbf;  mb.setMaskFigure(&mbf);  string filename2("types2");  mb.openFile(filename2);  mb.BuildMask();}

The builder mode applies:

1. When the construction process allows the constructed object to have different representations

2. The process of creating complex objects is independent of the composition of their objects.

The builder mode has the following advantages:

1. The internal representation of the product can be dynamically changed. Different generators have different representations.

2. Separate the constructed code and the representation code to improve the reusability and reusability of objects.

3. Precisely control the construction process. Builder builds the entire object step by step and finally gets the result. The intermediate process can be precisely controlled.

(End)

Design Pattern Generation generator Pattern

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.