Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Singleton component design problem

Singleton component design problem

Scheduled Pinned Locked Moved C#
questioncsharpcomdesignhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leppie
    wrote on last edited by
    #1

    Hi This one has me completely confused. What is the correct way of doing this? I have some "rules" I want to stick with. - Designer support - Object must be a singleton Now these 2 are conflicting :| Here's my possible solution:

    public class Singleton : Component
    {
    private static SingletonInternal instance = null;
    private static int instancecount = 0;

    private class SingletonInternal : IDisposable
    {
    //just the normal "instance" implementation

      public void SomeMethod()
      {
      }
    
      public void Dispose()
      {
         // free unmanaged resources
      }
    

    }

    public Singleton()
    {
    if (instance == null)
    instance = new SingletonInternal();
    instancecount++;
    }

    public void SomeMethod()
    {
    instance.SomeMethod();
    }

    protected override void Dispose(bool disposing)
    {
    if (!this.disposed)
    {
    if (disposing)
    {
    // free managed resources
    }
    instancecount--;
    if (instancecount == 0)
    instance.Dispose();

         this.disposed = true;
      }
    

    }
    }

    Is this the way to go? All comments welcome :) Cheers MyDUMeter: a .NET DUMeter clone
    "Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."

    P 1 Reply Last reply
    0
    • L leppie

      Hi This one has me completely confused. What is the correct way of doing this? I have some "rules" I want to stick with. - Designer support - Object must be a singleton Now these 2 are conflicting :| Here's my possible solution:

      public class Singleton : Component
      {
      private static SingletonInternal instance = null;
      private static int instancecount = 0;

      private class SingletonInternal : IDisposable
      {
      //just the normal "instance" implementation

        public void SomeMethod()
        {
        }
      
        public void Dispose()
        {
           // free unmanaged resources
        }
      

      }

      public Singleton()
      {
      if (instance == null)
      instance = new SingletonInternal();
      instancecount++;
      }

      public void SomeMethod()
      {
      instance.SomeMethod();
      }

      protected override void Dispose(bool disposing)
      {
      if (!this.disposed)
      {
      if (disposing)
      {
      // free managed resources
      }
      instancecount--;
      if (instancecount == 0)
      instance.Dispose();

           this.disposed = true;
        }
      

      }
      }

      Is this the way to go? All comments welcome :) Cheers MyDUMeter: a .NET DUMeter clone
      "Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."

      P Offline
      P Offline
      pete mcquain
      wrote on last edited by
      #2

      I think you want something along these lines... public class Singleton : Component { private Singleton m_instance = null; private Singleton(){} // constructor is private so it can't be created externally public static Instance() { if(!m_instance) m_instance = new Singleton; return m_instance; } } Your instance counting and dispose logic should still work. To use it, you dont create a new Singleton, but rather use Singleton s = Singleton.Instance();. -pete

      L 1 Reply Last reply
      0
      • P pete mcquain

        I think you want something along these lines... public class Singleton : Component { private Singleton m_instance = null; private Singleton(){} // constructor is private so it can't be created externally public static Instance() { if(!m_instance) m_instance = new Singleton; return m_instance; } } Your instance counting and dispose logic should still work. To use it, you dont create a new Singleton, but rather use Singleton s = Singleton.Instance();. -pete

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        pete mcquain wrote: private Singleton(){} // constructor is private so it can't be created externally That is where it wont work in a designer... MyDUMeter: a .NET DUMeter clone
        "Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups