Singleton Design Pattern Question 
Author Message
 Singleton Design Pattern Question

I have used the singleton design pattern extensively in Java and would like
to use it in C#.   See the following code for my example.  I can see where
the Singleton object is created but when is it destroyed?  In java, a
Singleton object was destroy when the VM died.  When does the .NET VM start
and when does it die (reboot)?

using System;
using System.Collections;

namespace Test
{
  public class Singleton {
      private static Hashtable ht = new Hashtable();
      private static Singleton INSTANCE = new Singleton();

      //constructor is private so external objects can not create a new
instance
      private Singleton(){
       Console.WriteLine("New instance of Singleton");
       ht.Add("K1","This is the value for Key1");
       ht.Add("K2","This is the value for Key2");
       ht.Add("K3","This is the value for Key3");
      }

      public static String getValue(String key){
       return (String)ht[key];
      }
 }

Quote:
}



Mon, 28 Jun 2004 03:24:26 GMT  
 Singleton Design Pattern Question
The singleton object will be "destroyed" when:
- the application domain containing the class gets unloaded
- or, when the CLR unloads,  that is when the application ends.

Willy.

Quote:

> I have used the singleton design pattern extensively in Java and would like
> to use it in C#.   See the following code for my example.  I can see where
> the Singleton object is created but when is it destroyed?  In java, a
> Singleton object was destroy when the VM died.  When does the .NET VM start
> and when does it die (reboot)?

> using System;
> using System.Collections;

> namespace Test
> {
>   public class Singleton {
>       private static Hashtable ht = new Hashtable();
>       private static Singleton INSTANCE = new Singleton();

>       //constructor is private so external objects can not create a new
> instance
>       private Singleton(){
>        Console.WriteLine("New instance of Singleton");
>        ht.Add("K1","This is the value for Key1");
>        ht.Add("K2","This is the value for Key2");
>        ht.Add("K3","This is the value for Key3");
>       }

>       public static String getValue(String key){
>        return (String)ht[key];
>       }
>  }
> }



Mon, 28 Jun 2004 04:02:45 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Singleton Design Pattern

2. Design Question: TreeView (GUI) & Composite Pattern (MODEL)

3. How to implement Singleton pattern

4. Destructors/Singleton Pattern

5. Globalness of Singleton pattern

6. Singleton design approach

7. Proper Singleton Design in C#

8. Design patterns

9. FS: Design Patterns (GoF) book

10. Looking for a design pattern...

11. C and design patterns

12. Design Patterns in C#

 

 
Powered by phpBB® Forum Software