D
Dave Townsend
John,
Schwartz counters are a variation of the Singleton pattern to control the
lifetime
of a shared resource such as a file stream such that the resource is created
only
once on the first use, and destroyed when the last client of the resource is
destroyed.
A mgr class controls the shared resource and keeps track of how many times
the constructor
for the resource is called and how many times the destructor is called.
Here is an example to explain better than my words...
dave
#include <iostream>
#include <string>
using namespace std;
class SharedResource
{
public:
SharedResource()
{
cout << "Create shared resource\n";
}
~SharedResource()
{
cout << "Closing shared resource \n";
}
void printmsg( const string& Message )
{
cout << "SharedResource::Msg " << Message << "\n";
}
};
class SharedResourceMgr
{
public:
SharedResourceMgr()
{
if ( !_count)
{
_SharedResource = new SharedResource();
}
_count++;
}
~SharedResourceMgr()
{
_count--;
if (!_count )
{
delete _SharedResource;
}
}
SharedResource* getSharedResource(){ return _SharedResource;}
private:
static long _count;
static SharedResource* _SharedResource;
};
long SharedResourceMgr::_count =0;
SharedResource* SharedResourceMgr::_SharedResource = 0;
class C1
{
public:
SharedResourceMgr _foo;
void printmsg( const string& msg){
_foo.getSharedResource()->printmsg(msg);}
};
class C2
{
public:
SharedResourceMgr _foo;
void printmsg( const string& msg){
_foo.getSharedResource()->printmsg(msg); };
};
class C3
{
public:
SharedResourceMgr _foo;
void printmsg( const string& msg){
_foo.getSharedResource()->printmsg(msg); };
};
// static instances
C1 c1;
C1 c2;
C1 c3;
int main(int argc, char* argv[])
{
c1.printmsg("C1");
c2.printmsg("C2");
c3.printmsg("C3");
return 0;
}
Schwartz counters are a variation of the Singleton pattern to control the
lifetime
of a shared resource such as a file stream such that the resource is created
only
once on the first use, and destroyed when the last client of the resource is
destroyed.
A mgr class controls the shared resource and keeps track of how many times
the constructor
for the resource is called and how many times the destructor is called.
Here is an example to explain better than my words...
dave
#include <iostream>
#include <string>
using namespace std;
class SharedResource
{
public:
SharedResource()
{
cout << "Create shared resource\n";
}
~SharedResource()
{
cout << "Closing shared resource \n";
}
void printmsg( const string& Message )
{
cout << "SharedResource::Msg " << Message << "\n";
}
};
class SharedResourceMgr
{
public:
SharedResourceMgr()
{
if ( !_count)
{
_SharedResource = new SharedResource();
}
_count++;
}
~SharedResourceMgr()
{
_count--;
if (!_count )
{
delete _SharedResource;
}
}
SharedResource* getSharedResource(){ return _SharedResource;}
private:
static long _count;
static SharedResource* _SharedResource;
};
long SharedResourceMgr::_count =0;
SharedResource* SharedResourceMgr::_SharedResource = 0;
class C1
{
public:
SharedResourceMgr _foo;
void printmsg( const string& msg){
_foo.getSharedResource()->printmsg(msg);}
};
class C2
{
public:
SharedResourceMgr _foo;
void printmsg( const string& msg){
_foo.getSharedResource()->printmsg(msg); };
};
class C3
{
public:
SharedResourceMgr _foo;
void printmsg( const string& msg){
_foo.getSharedResource()->printmsg(msg); };
};
// static instances
C1 c1;
C1 c2;
C1 c3;
int main(int argc, char* argv[])
{
c1.printmsg("C1");
c2.printmsg("C2");
c3.printmsg("C3");
return 0;
}