Globalness of Singleton pattern 
Author Message
 Globalness of Singleton pattern

I doubt that globalness is real word, but here's my question:
The  method espoused by Scott Meyers and others for having a single instance
of a class includes the following ideas:

class CMyClass
{
    CMyClass(){}
public:
    static CMyClass& MyClass();

Quote:
}

static CMyClass& CMyClass::MyClass()
{
    static CMyClass myclass;
    return myclass;

Quote:
}

My concern is how to adapt this method so that the instance of this class is
not globally available.  Any rogue code maintainer (such as myself) in the
same namespace can call CMyClass::MyClass().  Can this method be adapted to
restrict access to the instance such that any user of the instance must get
it from a composing class?

class CGateKeeper
{
public:
    m_MyClass* mineallmine;
    CGateKeeper(){m_MyClass = &CMyClass::MyClass();}

Quote:
}

I think I should keep my 'almost globals' from roaming freely.  Granted, the
above example doesn't use an accessor, but at least I can thus somewhat
group all my 'almost global' instances together.


Tue, 12 Jul 2005 06:25:34 GMT  
 Globalness of Singleton pattern

    Make MyClass() private (actually, it becomes unnecessary).  Make
CGateKeeper a friend of CMyClass.

--
Truth,
James Curran
www.noveltheory.com (personal)
www.njtheater.com (professional)

Quote:
> I doubt that globalness is real word, but here's my question:
> The  method espoused by Scott Meyers and others for having a single
instance
> of a class includes the following ideas:

> class CMyClass
> {
>     CMyClass(){}
> public:
>     static CMyClass& MyClass();
> }

> static CMyClass& CMyClass::MyClass()
> {
>     static CMyClass myclass;
>     return myclass;
> }

> My concern is how to adapt this method so that the instance of this class
is
> not globally available.  Any rogue code maintainer (such as myself) in the
> same namespace can call CMyClass::MyClass().  Can this method be adapted
to
> restrict access to the instance such that any user of the instance must
get
> it from a composing class?

> class CGateKeeper
> {
> public:
>     m_MyClass* mineallmine;
>     CGateKeeper(){m_MyClass = &CMyClass::MyClass();}
> }

> I think I should keep my 'almost globals' from roaming freely.  Granted,
the
> above example doesn't use an accessor, but at least I can thus somewhat
> group all my 'almost global' instances together.



Tue, 12 Jul 2005 06:43:25 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Singleton Design Pattern Question

2. Singleton Design Pattern

3. How to implement Singleton pattern

4. Destructors/Singleton Pattern

5. Singleton not a singleton?

6. ATL Singleton and Singleton Class also from C++

7. Pattern Definition GREP With Pattern Editing

8. Regular Expressions/Pattern Matching/Unordered pattern

9. new Singleton() ( Can I override the new operator to return a singleton?)

10. Pleading for fresh eyes on my problem using a PropertyGrid with a Singleton

11. Singletons & delete error

12. singleton class please help

 

 
Powered by phpBB® Forum Software