建造者模式原理:建造模式主要是用于产生对象的各个组成部分,而抽象工厂模式则用于产生一系列对象,建造者模式而且要求这些对象的组成部分有序。
代码如下:
#includeusing namespace std;class Builder{public: virtual void buildHead(){} virtual void buildBody(){} virtual void buildLeftArm(){} virtual void buildRightArm(){} virtual void buildLeftLeg(){} virtual void buildRightLeg(){}};class BuilderFat:public Builder{public: virtual void buildHead(){cout << "fat head" < buildHead(); pBuilder->buildBody(); pBuilder->buildLeftArm(); pBuilder->buildRightArm(); pBuilder->buildRightArm(); pBuilder->buildLeftLeg(); pBuilder->buildRightLeg(); }private: Builder* pBuilder;};int main(){ BuilderFat fat; Director *pDirector = new Director(&fat); pDirector->createPerson(); delete pDirector; return 0;}