//简单工厂模式 class CDisplayer { public: virtual void Play() = 0; }; class CVCDDisplayer : public CDisplayer { public: void Play(){ cout<<"vcd displayer"<<endl; } }; class CDVDDisplayer : public CDisplayer { public: void Play(){ cout<<"dvd displayer"<<endl; } }; class IFactory { public: static CDisplayer* CreateDisplayer(char szName) { switch ( szName ) { case 'v': { return new CVCDDisplayer(); } break; case 'd': { return new CDVDDisplayer(); } break; default:break; } return NULL; } }; int _tmain(int argc, _TCHAR* argv[]) { CDisplayer* pDisplayer = IFactory::CreateDisplayer('v'); pDisplayer->Play(); delete pDisplayer; } //抽象工厂模式 class IUser { public: virtual void fun() = 0; }; class SqlUser : public IUser { public: void fun(){cout<<"Sql USER"<<endl;} }; class AcessUser : public IUser { public: void fun(){cout<<"access USER"<<endl;} }; class IFactroy2 { public: virtual IUser* createUser() = 0; }; class SQLFactroy : public IFactroy2 { public: IUser* createUser() { return new SqlUser; } }; class AcessFactroy : public IFactroy2 { public: IUser* createUser(){return new AcessUser;} }; int _tmain(int argc, _TCHAR* argv[]) { IFactroy2* pFactory = new SQLFactroy(); IUser* pUser = pFactory->createUser(); pUser->fun(); delete pUser; delete pFactory; } 转自:http://edu.codepub.com/2009/0917/15496.php http://hi.baidu.com/datapan/blog/item/1805a30f9f50c9206059f399.html