Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

Singleton / Function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Christian - 02 Dec 2004 18:29 GMT
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
Cowboy (Gregory A. Beamer) - MVP - 02 Dec 2004 21:33 GMT
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!
***************************

> Hi,
> I have an public interface ILocal, and a private class (Local) derived from
[quoted text clipped - 30 lines]
> Tell me if there are a better way to do that.. If not, what i did wrong!?
> Thanks
Jon Skeet [C# MVP] - 03 Dec 2004 08:46 GMT
Cowboy (Gregory A. Beamer) - MVP <NoSpamMgbworld@comcast.netNoSpamM>
wrote:
> 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
[quoted text clipped - 26 lines]
>     }
> }

That's not thread-safe - see
http://www.pobox.com/~skeet/csharp/singleton.html
for various thread-safe versions.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Christian - 03 Dec 2004 16:29 GMT
Thanks for your anwser!
But can you anwser to this part !?
I am right?! if yes, how to implement in cpp?!
thanks

> > ...... But with this, others modules can call directly the other public
> > function (ex: GetValue() and SetValue() derived from ILocal). Someone tell me
[quoted text clipped - 11 lines]
> > But i wasnt able to found how to implement this functions in the .cpp! All
> > the patterns i tried doent work!

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.