The Singleton mode and Singleton mode are briefly studied.

Source: Internet
Author: User

The Singleton mode and Singleton mode are briefly studied.

I used to take notes, but I didn't have the habit of writing articles. Today I am studying and learning. I just want to record the process and everything. Maybe it can help one or two friends.

First, let's think about what is a singleton. As the name suggests, what are the benefits of a singleton model for a single object? For example, your object can only be instantiated once.

First, write a simple test example. For example, if I create a class named TestSingle

  

1 /// <summary> 2 // simple example of Singleton mode (sealed, not inherited) 3 /// </summary> 4 public sealed class TestSingle5 {6}

First, we need to make this class non-inheritance, otherwise it will be meaningless. So what will we do next? Let an object be instantiated only once, thus reducing and so on. There are a lot of professional terms, which can be seen by Baidu sousuo. What will we do when instantiating an object? It must be a new object. What will happen to the new object? Is to execute the constructor. How can we instantiate this class only once? It looks like you can only start with the constructor.

  

1 // <summary> 2 // records the number of constructor executions. 3 // </summary> 4 private static int structureCount = 0; 5 6 /// <summary> 7 // private parameter-free constructor 8 /// </summary> 9 private TestSingle () 10 {11 structureCount ++; 12 Console. writeLine ("only the number of {0} constructor executions", structureCount); 13}

First, I defined a static variable to store the number of constructor executions and output the number of executions in the constructor. After I private the constructor, how can I access the outside world in other ways? Let's write a public method to provide external calls. Of course, this method is also a static method.

1 /// <summary> 2 /// here 3 is executed during instantiation // </summary> 4 private static int createStructureCount = 0; 5 6 /// <summary> 7 // TestSingle 8 /// </summary> 9 private static TestSingle testSingle = null; 10 11 12 /// <summary> 13 // create a testSingle instance 14 /// </summary> 15 /// <returns> </returns> 16 public static testSingle CreateTestSingle () 17 {18 createStructureCount ++; 19 Console. writeLine ("I am creating TestSingle instantiation for {0} Times", createStructureCount); 20 testSingle = new TestSingle (); 21 return testSingle; 22}

What is the difference between writing in this way and a common new one? Let's modify the method.

1 /// <summary> 2 /// create a testSingle instance 3 /// </summary> 4 /// <returns> </returns> 5 public static TestSingle CreateTestSingle () 6 {7 createStructureCount ++; 8 Console. writeLine ("I am creating TestSingle instantiation for {0} Times", createStructureCount); 9 if (testSingle = null) 10 {11 testSingle = new TestSingle (); 12} 13 return testSingle; 14}

Let's perform a test to see how it works.

We can see that the constructor has been executed twice, and the CPU performance of my computer is not very good. If it is better, it may be executed more times. So how did I perform the test? I wrote a thread factory and kept going to CreateTestSingle

1 static void Main (string [] args) 2 {3 // create a Task Factory 4 TaskFactory taskFactory = new TaskFactory (); 5 for (int I = 0; I <20; I ++) 6 {7 taskFactory. startNew () => TestSingle. createTestSingle (); 8} 9 Console. readLine (); 10}

That is to say, when multiple threads go to Create at the same time, we may as well add multiple objects. So this is not the case, so I added a lock and dual-layer judgment.

  

1 /// <summary> 2 // Lock object 3 /// </summary> 4 private static object lock_SingleTest = new object (); 5 6 /// <summary> 7 // create a testSingle instance 8 /// </summary> 9 /// <returns> </returns> 10 public static TestSingle createTestSingle () 11 {12 if (testSingle = null) 13 {14 lock (lock_SingleTest) 15 {16 if (testSingle = null) 17 {18 testSingle = new TestSingle (); 19} 20 21} 22} 23 createStructureCount ++; 24 Console. writeLine ("I am creating TestSingle instantiation for {0} Times", createStructureCount); 25 return testSingle; 26}

Run once again

In this way, the constructor will be executed only once no matter how many times I execute it. You can let the thread sleep and print the result again, so that I think you can see the effect. So we have created the singleton mode.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.