|
|
|
|
|
by palish
5534 days ago
|
|
A better question might be, "Would this pattern impact our ability to write unit tests?" I believe the answer is "No." Let's say the module Foo depends on the Graphics subsystem. That is, Foo.cpp has the code: #include "GrSubsys.h"
//-------------------------------------------------------
Foo::Foo( const string& name )
{
// fetch a handle to our model (loading it if necessary).
_model = gGrSubsys->GetModel( "models/" + name );
}
//-------------------------------------------------------
bool
Foo::IsValid()
{
return ( _model != NULL );
}
In order to write a unit test that takes into account the aforementioned Singleton pattern, you might write: //-------------------------------------------------------
void
Test_EngineComponents()
{
// prepare for science.
AppStartup();
//==========================
// Test #1 - Foo
//==========================
{
// load a Foo entity.
Foo* sunTzu = new Foo( "test/warlord" );
// verify the entity loaded successfully.
assert( sunTzu->IsValid() );
// shutdown.
delete sunTzu;
}
// conclude our science.
AppShutdown();
}
and AppStartup() is the function which initializes the subsystem singletons (and those will initialize their manager singletons).It's about discipline. Any fool can butcher with any tool. |
|