Builder mode: Used primarily to create complex objects in which the built-in order of internal builds is usually stable, but the construction of objects within the object often faces complex changes. The advantage is that the construction code is separated from the presentation code, and because the builder hides how the product is assembled, the internal representation of a product needs to be changed, and only one concrete builder can be defined. The builder pattern is when the algorithm that creates the complex object should be independent of the part of the object and the mode in which they are assembled.
#ifndef builder_h#define bulder_h#include<iostream>//#include <windows.graphics.printing.h>using namespace Std;class person{public:virtual void Buildhead () =0;virtual void buildbody () = 0;virtual void Buildarmleft () = 0; virtual void buildarmright () = 0;virtual void Buildlegleft () = 0;virtual void Buildlefrighy () = 0;}; Class Fatperson:p ublic person{public:void buildhead () {cout << "fathead\n";} void Buildbody () {cout << "fatbody\n";} void Buildarmleft () {cout << "Fat armleft\n";} void Buildarmright () {cout << "Fat armright\n";} void Buildlegleft () {cout << "Fat legleft\n";} void Buildlefrighy () {cout << "Fat legright\n";}}; Class Thinperson:p ublic person{public:void buildhead () {cout << "thinhead\n";} void Buildbody () {cout << "thinbody\n";} void Buildarmleft () {cout << "Thin armleft\n";} void Buildarmright () {cout << "Thin armright\n";} void Buildlegleft () {cout << "Thin legleft\n";} void Buildlefrighy () {cout << "Thin LegrigHt\n ";}}; Class Persondirector{person * Per;public:persondirector (person * m):p er (m) {}void Creatperson () {Per->buildarmleft ( );p er->buildarmright ();p er->buildbody ();p er->buildhead ();p er->buildlefrighy ();p Er->buildlegleft ();}}; #endif
#include "Bulder.h" int main () {person * F = new Fatperson; Persondirector PDf (F); Pdf.creatperson (); Person * T = new Thinperson; Persondirector PDt (T); Pdt.creatperson (); return 0;}
Design pattern C + + Implementation IX: Builder mode