// builderTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" //实现类的抽象基类,定义了实现Abastraction的基本操作,而它的派生类实现这些接口 class Implementor { public: virtual void Operation(void) = 0; }; class CAImplementor : public Implementor { public: virtual void Operation(void) { //... } }; class CBImplementor : public Implementor { public: virtual void Operation(void) { //... } }; //抽象类,它的实现方式由Implementor完成 class Abstraction { public: Abstraction(Implementor* pImplementor) : m_pImplementor(pImplementor) { } void Operation(void) { m_pImplementor->Operation(); } protected: Implementor* m_pImplementor; }; int _tmain(int argc, _TCHAR* argv[]) { //接口和实现分离 Implementor* pAImplementor = new CAImplementor; Implementor* pBImplementor = new CBImplementor; Abstraction* pAbstraction0 = new Abstraction(pAImplementor); pAbstraction0->Operation(); Abstraction* pAbstraction1 = new Abstraction(pBImplementor); pAbstraction1->Operation(); delete pAImplementor; delete pBImplementor; delete pAbstraction0; delete pAbstraction1; return 0; }