Rob 的个人资料Software.照片日志列表更多 ![]() | 帮助 |
|
8月6日 The factory pattern.After reading a few explanations of the factory design pattern and viewing some not so simple examples i thought id try an explain it.
Why use it?
Flexible code and can be used to encapsulate code if the creation of an object is complicated.
Psuedo code example shows how it might be used in a cross platform graphics library.
class GenericFactory
{ GenericFactory * getFactory() { if(MyOs() == Windows) return(new WindowsFactory); if(MyOs() == Linux)
return(new LinuxFactory); } virtual void drawbutton();
}; class WindowsFactory:GenericFactory
{ drawbutton() { Do some windows specific stuff } }; class LinuxFactory:GenericFactory
{ drawbutton() { Do some Linux specific stuff } }; int main()
{ GenericFactory genfactory ; GenericFactory * Factory = genfactory.getFactory();
Factory.drawButton(); } 引用通告此日志的引用通告 URL 是: http://robsgames.spaces.live.com/blog/cns!3145B5828D53FBD9!160.trak 引用此项的网络日志
|
|
|