简体   繁体   中英

Do two instances of a singleton class have the same property value?

I am new to design patterns in C#. Can anyone please give me some instructions about the implementation of a Singleton class. I just implemented a tutorial but I am not able to understand use of singleton class with this "singleton means we can create only one instance of a class". Then why we don't access property which is written in the singleton class using two different instance of the class.

Please look at my code and give me instructions about the mistake I made.

static void Main(string[] args)
{

     Singleton instance = Singleton.getInstance();
     instance.Message = "Text Message";

     Singleton instance1 = Singleton.getInstance();
     Console.WriteLine(instance.Message);
     Console.WriteLine(instance1.Message);
     Console.ReadKey();
}

class Singleton
{
    private static Singleton singleton=null;
    private Singleton(){}
    public static Singleton getInstance()
    {
        if (singleton!=null)
        {
            return singleton;
        }
        return new Singleton();
    }
    public string Message{get; set;}
}

Your singleton is incorrect. More correct version:

class Singleton
{
    private static Singleton singleton = null;
    private Singleton(){}
    public static Singleton getInstance()
    {
        if (singleton!=null)
        {
            return singleton;
        }
        return (singleton = new Singleton()); //here is
    }
    public string Message{get; set;}
}

And very good solution:

class Singleton
{
    private static Lazy<Singleton> singleton = new Lazy<Singleton>(()=> new Singleton());
    private Singleton() { }
    public static Singleton getInstance()
    {
        return singleton.Value;
    }
    public string Message { get; set; }
}

It has no problems with thread-safity and lazy initialization.

By default, all public and protected members of the Lazy class are thread safe and may be used concurrently from multiple threads. These thread-safety guarantees may be removed optionally and per instance, using parameters to the type's constructors.

Your Singleton implementation is incorrect.
A Singleton is designed to only allow none or a single instance at all times.
This is where you went wrong:

public static Singleton getInstance()
{
   // "singleton" will always be null.
   if (singleton != null)
   {
      return singleton;
   }

   // Always returns new instance rather than existing one.
   return new Singleton();
}

To fix it you should write:

public static Singleton getInstance()
{
   // Return the instance we might have stored earlier.
   if (singleton != null)
      return singleton;

   // Now we store the only instance that will ever be created.
   singleton = new Singleton();
   return singleton;
}

Note that this is not thread safe if called multiple times in parallel.

As a resource I can recommend Jon Skeet's post:
http://csharpindepth.com/Articles/General/Singleton.aspx

He explaines six different solutions (including thread-safety and the Lazy(T)) to the Singleton Pattern and how to code them.

Does two instance of singleton class has a same property value?

The answer to your question is yes, they has the same property value. Important thing is understand why, and the reason is the core of what a singleton is. So, why?:

Because you are confusing two references with two instance.

There are no two instances, there are always one none or one instance of the singleton class.

In your code, singleton variable and singleton1 variable are pointing both to the same object, the singleton, and the reason is because of the implementation of the method getInstance(), is simple to understand :

If method is called for the very first time, then it creates for unique time the singleton object with method new.

If method is called after a first time, it will return the singleton object created in the first call of method.

So, no matter how many variables of type Singleton you have, you will always have only one Singleton created with the new method, the instance, the singleton instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM