C # static class Singleton Comparative

Company classes need to achieve single mode embodiment, this can save resources, to avoid duplicate object generated. However, a static class can do this, and to write more concise, so access to relevant information, hoping to find out the difference between the two.

1. Singleton can be initialized when used, and static class initialization program will start running?

Through their own practice, I think this argument is wrong. I wrote the following examples of experiments.

    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine(Class1.get(190));
            GC.Collect();
            Console.ReadKey();
        }
    }
    public static class Class1
    {
        public static int[] arr = new int[20480000];

        static Class1()
        {
            for (int i = 1; i < arr.Length; i++)
            {
                arr[i] = i;
            }
        }

        public static int get(int i)
        {
            return arr[i];
        }
    }

 

When the program enters the main function, this memory is

Obviously static class arr has not been initialized, continue

  

Then you can see static class is initialized arr, arr and time values ​​are all 0, the memory does not change, is it that Microsoft is optimized?

2.GOF on a single piece of a scene interpretation is, "If you need a unique instance"?

Amount, is not the only instance of a static class Is it?

3. Singleton, decoupling?

 

public  interface IHelloWorld 
{ 
    String the HelloWorld (); 
} 
 
// then implement two interfaces. 
public  class HelloWorldEnglish: IHelloWorld 
{ 
    String IHelloWorld.HelloWorld () 
    { 
        return  " the Hello World! " ; 
    } 
} 
public  class HelloWorldChinese: IHelloWorld 
{ 
    String IHelloWorld .HelloWorld () 
    { 
        return  " hello World! " ; 
    } 
} 

public  class People 
{ 
    Private Readonly IHelloWorld peopleHelloWorld;
     public People (IHelloWorld the helloWorld) 
    { 
        the this .peopleHelloWorld = CreateSingle ()
 // where you can use the model to create a single embodiment or out HelloWorldEnglish HelloWorldChinese 
    }
     public  void the SayHello () 
    { 
        Console.WriteLine ( the this .peopleHelloWorld.HelloWorld () ); 
    } 
}

This code I have read Leng Shimo understand what relationship decoupling and single cases, but added understand interface decoupling is very good (laugh cry manual), the following Heroes same view with me, but still did not answer my doubts

 

 Example 4. Single inheritance can realize the interface, and not static class

I quite agree with this view, if you want to use a single example, want loosely coupled, it will use the Singleton pattern.

Static class more suitable tools.


Reference links:

https://bbs.csdn.net/topics/390487790

https://www.cnblogs.com/zhangzt/p/4350556.html

https://bbs.csdn.net/topics/390825682

https://www.xin3721.com/ArticlecSharp/c13696.html

Guess you like

Origin www.cnblogs.com/wintertone/p/11515285.html