Builder mode: Separates the construction of a complex object from its representation so that the same build process can create different representations.
Pattern implementation:
[Code]class builder;//Product class product{public:void Addpart (const char *info) {m_partinfovec.push_back (info); } void Showproduct () {for (std::vector<const char *>::iterator item = M_partinfovec.begin (); Item! = M_par Tinfovec.end (); ++item) {std::cout << *item << Std::endl; }}private:std::vector<const Char *> m_partinfovec;};/ /builder Builder, Unified abstract interface class builder{public:virtual void Buildparta () {} virtual void Buildpartb () {} virtual product* GetProduct () {return null;}};/ /Concrete Created Object method class Concretebuilder:public Builder{public:concretebuilder () {m_product = new Product ();} void Buildparta () {M_product->addpart ("Parta completed"); } void Buildpartb () {m_product->addpart ("PartB Q"); } product* GetProduct () {return m_product; }private:product *m_product;};/ /director controls the creation of the concrete Construction object class Director{public:director (Builder *builder) {m_builder = builder;} void CreAteproduct () {M_builder->buildparta (); M_BUILDER->BUILDPARTB (); }private:builder *m_builder;};
Client:
[Code]int Main () { Builder *builderobj = new ConcreteBuilder (); Director directorobj (builderobj); Directorobj.createproduct (); Product *productobj = Builderobj->getproduct (); if (productobj = = NULL) { return 0; } Productobj->showproduct (); Delete productobj; Productobj = NULL; Delete builderobj; Builderobj = NULL;}
The above is the C + + design mode of knowledge of the content of the builder model, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!