Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using singleton instead of a global static instance

Tags:

c#

singleton

I ran into a problem today and a friend recommended I use a global static instance or more elegantly a singleton pattern. I spent a few hours reading about singletons but a few things still escape me.

Background: What Im trying to accomplish is creating an instance of an API and use this one instance in all my classes (as opposed to making a new connection, etc).

There seems to be about 100 ways of creating a singleton but with some help from yoda I found some thread safe examples. ..so given the following code:

public sealed class Singleton
{
     public static Singleton Instance { get; private set; }

     private Singleton()
     {
        APIClass api = new APIClass();  //Can this be done?
     }

     static Singleton() { Instance = new Singleton(); }
}

How/Where would you instantiate the this new class and how should it be called from a separate class?

EDIT: I realize the Singleton class can be called with something like

Singleton obj1 = Singleton.Instance();

but would I be able to access the methods within the APIs Class (ie. obj1.Start)? (not that I need to, just asking)

EDIT #2: I might have been a bit premature in checking the answer but I do have one small thing that is still causing me problems. The API is launching just fine, unfortunately Im able to launch two instances?

New Code

public sealed class SingletonAPI
{
 public static SingletonAPI Instance { get; private set; }

 private SingletonAPI() {}

 static SingletonAPI() { Instance = new SingletonAPI(); }     

 // API method:
 public void Start() { API myAPI = new API();}
 }

but if I try to do something like this...

SingletonAPI api = SingletonAPI.Instance;
api.Start();
SingletonAPI api2 = SingletonAPI.Instance;  // This was just for testing.
api2.Start();

I get an error saying that I cannot start more than one instance.

like image 606
Ryan Avatar asked Apr 06 '10 22:04

Ryan


People also ask

Why we use Singleton design pattern instead of static?

A Singleton can implement interfaces, inherit from other classes and allow inheritance. While a static class cannot inherit their instance members. So Singleton is more flexible than static classes and can maintain state. A Singleton can be initialized lazily or asynchronously and loaded automatically by the .

What is the reason to use a singleton instead of a class with only static members and never instantiate any object of that class?

The Singleton pattern has several advantages over static classes. First, a singleton can extend classes and implement interfaces, while a static class cannot (it can extend classes, but it does not inherit their instance members).

Why you should not use singletons?

By using singletons in your project, you start to create technical debt. Singletons tend to spread like a virus because it's so easy to access them. It's difficult to keep track of where they're used and getting rid of a singleton can be a refactoring nightmare in large or complex projects.

What is the disadvantages of using singleton?

Singletons hinder unit testing: A Singleton might cause issues for writing testable code if the object and the methods associated with it are so tightly coupled that it becomes impossible to test without writing a fully-functional class dedicated to the Singleton.


1 Answers

Why not just add a public APIClass property to your singleton?

public sealed class Singleton
{
     public static Singleton Instance { get; private set; }

     private APIClass _APIClass; 

     private Singleton()
     {
        _APIClass = new APIClass();  
     }

     public APIClass API { get { return _APIClass; } }

     static Singleton() { Instance = new Singleton(); }     
}

Then your calling site looks like:

Singleton.Instance.API.DoSomething();

Or if you are the author of the API class, you could make it a singleton itself, instead of wrapping it in a singleton:

public sealed class SingletonAPI
{
     public static SingletonAPI Instance { get; private set; }

     private SingletonAPI() {}

     static SingletonAPI() { Instance = new SingletonAPI(); }     

     // API method:
     public void DoSomething() { Console.WriteLine("hi"); }
}

API call:

SingletonAPI.Instance.DoSomething();
like image 90
Igby Largeman Avatar answered Sep 29 '22 08:09

Igby Largeman