How to implement Singleton pattern 
Author Message
 How to implement Singleton pattern

I found two ways to implement the singleton pattern.
1)
sealed class Singleton1
{
    private Singleton1();
    public static readonly Singleton Instance = new Singleton1();
    public void MyMethod()
    {
        //doSomething
    }

Quote:
}

2)
sealed class Singleton2
{
    static Singleton2();
    public void MyMethod()
    {
        //doSomething
    }

Quote:
}

using it looks like
{
    Singleton1.Instance.MyMethod();
    Singleton2.MyMethod();

Quote:
}

I'm not sure if the second way has the same thread safety as the first one.
Does anybody see a difference (except the calling) between the two way's.
Which is the better one?

tnx for your opinion
Markus



Sat, 16 Oct 2004 17:02:42 GMT  
 How to implement Singleton pattern
In first way you have more control when instance created
or marked to finalization. For example, you can
implement "lazy" construction.

In second way you can't define indexer, becouse C# have no
concept of static indexer.

But, of course, second way is more elegant.



Sat, 16 Oct 2004 17:39:11 GMT  
 How to implement Singleton pattern
Markus,

    This might not be possible with your setup, but you might want to
consider COM+ and a serviced component for what you want to do.  Using COM+,
you can create an object pool for your object.  By setting the pool minimum
to 1 and the maximum to 1, you can effectively create a singleton.  Also,
using COM+, you can manage concurrency (activity and causalities).

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> I found two ways to implement the singleton pattern.
> 1)
> sealed class Singleton1
> {
>     private Singleton1();
>     public static readonly Singleton Instance = new Singleton1();
>     public void MyMethod()
>     {
>         //doSomething
>     }
> }

> 2)
> sealed class Singleton2
> {
>     static Singleton2();
>     public void MyMethod()
>     {
>         //doSomething
>     }
> }

> using it looks like
> {
>     Singleton1.Instance.MyMethod();
>     Singleton2.MyMethod();
> }

> I'm not sure if the second way has the same thread safety as the first
one.
> Does anybody see a difference (except the calling) between the two way's.
> Which is the better one?

> tnx for your opinion
> Markus



Sat, 16 Oct 2004 20:26:02 GMT  
 How to implement Singleton pattern
No 1, but you have to make some changes or it won't compile.

sealed class Singleton1{
  private Singleton1() {}
  public static readonly Singleton1 Instance = new Singleton1();
  public void MyMethod()
  {
    // do something!
  }

Quote:
}

Willy.
Quote:

> I found two ways to implement the singleton pattern.
> 1)
> sealed class Singleton1
> {
>     private Singleton1();
>     public static readonly Singleton Instance = new Singleton1();
>     public void MyMethod()
>     {
>         //doSomething
>     }
> }

> 2)
> sealed class Singleton2
> {
>     static Singleton2();
>     public void MyMethod()
>     {
>         //doSomething
>     }
> }

> using it looks like
> {
>     Singleton1.Instance.MyMethod();
>     Singleton2.MyMethod();
> }

> I'm not sure if the second way has the same thread safety as the first one.
> Does anybody see a difference (except the calling) between the two way's.
> Which is the better one?

> tnx for your opinion
> Markus



Sat, 16 Oct 2004 22:20:42 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Singleton Design Pattern Question

2. Singleton Design Pattern

3. Destructors/Singleton Pattern

4. Globalness of Singleton pattern

5. How to implement Memento Pattern in C#

6. How to implement a Model View Controller design pattern in C# .NET

7. Implementing a Parameterized factory method pattern in c#...

8. Singleton not a singleton?

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

10. Pattern Definition GREP With Pattern Editing

11. Regular Expressions/Pattern Matching/Unordered pattern

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

 

 
Powered by phpBB® Forum Software