class IMusicBox { public: virtual void Play() = 0; } class PianoBox: public IMusicBox { public: virtual void Play(){printf("Play PianoBox");} } class ViolinBox: public IMusicBox { public: virtual void Play(){printf("Play ViolinBox");} } class MusicBoxFactory { public: static IMusicBox* CreateMusicBox(string name) { if(name == "PianoBox") { return new PianoBox; } else if(name == "ViolinBox") { return new ViolinBox; } return NULL; } } class MusicBoxDemo { public: void Main() { IMusicBox* pBox = MusicBoxFactory::CreateMusicBox("PianoBox"); if(NULL == pBox) {return;} pBox->Play(); SAFE_RELEASE(pBox); } } |
|
Client只面对SimpleFactory, 依赖于ProductInterface。产品的具体实现与客户隔开。