|
|
|
|
|
by palish
5534 days ago
|
|
As a video game programmer, I use the Singleton pattern quite often. For example, "TextureMgr", "MaterialMgr", "ModelMgr", "GrSubsys" (for Graphics Subsystem), etc.. The most beautiful Singleton code I've seen in C++ is: class GrSubsys
{
public:
GrSubsys();
~GrSubsys();
};
extern GrSubsys* gGrSubsys;
... then the constructor and destructor are written such that you can startup/shutdown the singleton as follows: //-------------------------------------------------------
void
App_Startup()
{
// initialize graphics subsystem.
new GrSubsys;
}
//-------------------------------------------------------
void
App_Shutdown()
{
// shutdown the graphics subsystem.
delete gGrSubsys;
}
//-------------------------------------------------------
int
main()
{
// startup the engine.
AppStartup();
// enter the per-frame application loop.
while ( AppFrame() )
{
}
// shutdown the engine.
AppShutdown();
return 0;
}
Shrug. A friend introduced it to me, and I liked it a lot. The same concept can be easily applied to C, too. |
|
some_system::get_instance()->MemberFunc(), instead of sys->MemberFunc()
Problem came apparent when somebody had made the Camera singleton, and it was singleton...
Nowadays we have one keyboard, one mouse, one printer... We used to have one DISPLAY, even one WINDOW (fullscreen), and one JOYSTICK. Just to get the point...