RE: Singleton / Function

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Cowboy (Gregory A. Beamer) - MVP (NoSpamMgbworld_at_comcast.netNoSpamM)
Date: 12/02/04


Date: Thu, 2 Dec 2004 13:33:03 -0800

The singleton pattern uses a private constructor a public static method and a
private static instance of itself to pass back from the method. Basic
signature (C#):

public class MySingleton : ILocal
{
   private MySingleton { }
   private static MySingleton _sing;
   private string _val;

   public MySingleton GetInstance()
   {
       if(_sing==null)
          _sing=new MySingleton();
 
       return _sing;
    }

    public string Value()
    {
         get
         {
              return _val;
         }
         set
         {
              _val=value;
         }
    }
}

That works for a single value that gets set. If you are creating an indexed
value, there is a variety of ways to handle it, like storing the value, by
name, in a hashtable and having the property return a single string value
from the hashtable. Since you already have the interface, you can link to an
interface method, but I am not sure you need an interface for what you are
attempting, unless you are using the Interfaces with other classes.

NOTE: If you are storing multiple values, you can create a singleton that
hosts a collection of objects instead of merely string vals.

---
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
"Christian" wrote:
> Hi,
> I have an public interface ILocal, and a private class (Local) derived from 
> this interface. The interface have 2 functions (GetValue() and SetValue()). 
> My object must be a singleton! How can i get a pointer to the unique instance 
> of my class!? I can't add a static function in the interface.. I can't add 
> global function.. I found a way.. (as describe later) but it have a better 
> way!??
> 
> ----
> 
> The solution is to set my class "Local" public, and add a static function to 
> my class. I set the contructor private, to prevent anyone to instance my 
> class. But with this, others modules can call directly the other public 
> function (ex: GetValue() and SetValue() derived from ILocal). Someone tell me 
> that i can prevent this if i declare function like this :
> 
> public __gc class Local : public ILocal
> {
>   ///Get the singleton
>   static ILocalPersistence* GetLocalPersistence();
> 
>   Object * ILocal::GetValue(String* _Name);
>   Void ILocal::SetValue(String* _Name, Object * _Value);
> }
> 
> But i wasnt able to found how to implement this functions in the .cpp! All 
> the patterns i tried doent work!
> 
> Object * Local::ILocal::GetValue(String* _Name)
> Object * Local::GetValue(String* _Name)
> Object * ILocal::Local::GetValue(String* _Name)
> 
> Tell me if there are a better way to do that.. If not, what i did wrong!?
> Thanks


Relevant Pages

  • Re: Updates to a single class instance
    ... I tried to use a singleton, ... public class Collector{ ... private static Collector ref; ...
    (comp.lang.java.programmer)
  • Re: globals?
    ... singleton http://en.wikipedia.org/wiki/Singleton_pattern. ... Public Module myGlobals ... Those are therefore private to the module, ... myglobals.instance.somedata="Test String" ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Running public IPs inside an RFC 1597 network
    ... > I'm running a typical Class C RFC 1597 network in my lab. ... know or care if we humans designate a subnet as public or private. ... is the absolute most general route there is for a machine. ... In a correctly configured system when you define an interface, ...
    (freebsd-questions)
  • Re: modular programming in Forth
    ... "The public definitions constitute the ... interface to codeexternal to the module" ... you can tinker at will with the source for the private words, ... to fixing a pesky bug. ...
    (comp.lang.forth)
  • Re: Object Oriented Content System - the idea
    ... Would probably be difficult to enforce this militant style in PHP. ... (It's java, but you probably don't have to know java to see what's going ... The advantage of keeping them private or protected has been to prevent using ... way of private inner class and interface) In PHP this typically means another ...
    (comp.lang.php)