#include "stdafx.h" #include <iostream> // 复杂对象 class CCompositeObject { public: void Print() { std::cout << "Composite Object" << std::endl; } public: void DoA() {} void DoB() {} void DoC() {} }; // 构造器 class CBuilder { public: virtual void BuildPartA() = 0; virtual void BuildPartB() = 0; virtual void BuildPartC() = 0; virtual CCompositeObject* GetObject() = 0; }; class CConcreteBuilder : public CBuilder { public: virtual void BuildPartA() { m_object.DoA(); } virtual void BuildPartB() { m_object.DoB(); } virtual void BuildPartC() { m_object.DoC(); } public: CCompositeObject* GetObject() { return &m_object; } private: CCompositeObject m_object; }; // 向导 class CDirector { public: CDirector(CBuilder* pBuilder) { pBuilder->BuildPartA(); pBuilder->BuildPartB(); pBuilder->BuildPartC(); } }; int _tmain(int argc, _TCHAR* argv[]) { CConcreteBuilder builder; CDirector director(&builder); CCompositeObject* pObject = builder.GetObject(); pObject->Print(); return 0; }