komodosp
komodosp

Reputation: 3658

C# Using a function to get instance instead of "new"

Getting used to some new code and have a question. All over it, I am seeing the following

file1.cs :

MyClass myInstance = MyClass.Instance();

...and then in the definition of MyClass there is...

MyClass.cs :

public class MyClass {

   // etc. etc.

   static readonly MyClass _instance = new MyClass();

   public static MyClass Instance() {
      return _instance;
   }

   // etc. etc.

}

What's the reason for doing that as opposed to just in file1.cs :

MyClass myInstance = new MyClass();

?

Upvotes: 0

Views: 2072

Answers (6)

Keysharpener
Keysharpener

Reputation: 504

I would advise you to use a property instead of a function. It is cleaner, more convenient for debugging and mock testing.

Note that by using a static constructor (as you are already doing), the initialization of _instanceis thread-safe.

Note : The implementation of the other methods should not be static.

Find below the complete implementation of the singleton pattern:

public class MyClass
{

    static MyClass _instance = new MyClass();

    public static MyClass Instance
    {
        get
        {
            return _instance;
        }
        set { _instance = value; }
    }

    public void DoStuff()
    {
         //etc...
    }

    private MyClass()
    {

    }
}

And you use it in other classes this way :

 MyClass.Instance.DoStuff();

Upvotes: 0

Christos
Christos

Reputation: 53958

This is the singleton pattern. That's why a method is used to get the instance.

Singleton pattern is used, when we want only one instance of a class be used in our app. We don't want the consumers of our app have the right to build more than one instances.

For a detailed description of the singleton pattern, please have a look here.

Upvotes: 1

t3chb0t
t3chb0t

Reputation: 18724

And yet another theory about the singleton...

You use it also in situations when you would like to have a common object shared by different parts of your application. Sometimes it might be necessary that you are forced to turn a static class into a singleton so that you can use if for data binding (a static class cannot be bound).

Some typical singletons that live as long as the application lives can be settings, some kind of cache, a backgrou file uploader with file watcher etc. etc...

Upvotes: 0

Rajdeep Dosanjh
Rajdeep Dosanjh

Reputation: 1187

This is a Singleton Pattern sometimes a new instance is not the best way to do something.

An example of this is a logger. There is no need to instantiate a logger every time one is needed hence the singleton pattern is useful.

Upvotes: 0

CaldasGSM
CaldasGSM

Reputation: 3062

it is a "singleton pattern" you can read about it here http://csharpindepth.com/Articles/General/Singleton.aspx

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176946

Because its using single ton pattern that is the reason...

Read : Singleton - The singleton pattern is a design pattern that is used to ensure that a class can only have one concurrent instance. Whenever additional objects of a singleton class are required, the previously created, single instance is provided.

Read : Singleton with proper example

Upvotes: 2

Related Questions