01 Singleton mode, 01 mode

Source: Internet
Author: User

01 Singleton mode, 01 mode

I. Introduction

My personal blog in the blog Park was applied in February, but I haven't written any articles yet. I feel a little sorry for this blog (a bit of installation .. Haha ). Well, let's not talk much about it. In this article, we will introduce the first one of the 23 design patterns-the singleton model.

Ii. Singleton Mode

When talking about the singleton mode, the first response should be-what is the singleton mode. For the singleton mode definition, the official definition is summarized in two sentences to ensure that a class has only one instance (that is, the class object ), and provides a global access point (the unique instance of this class is accessed externally through this access point ). Haha, isn't it a little embarrassing? It doesn't matter. It's normal! The Singleton mode has only two lines of code. The two sentences in the definition correspond to one sentence of Code respectively. I will not explain this definition here. We will use code to talk about it. Here I will create a new console application to introduce the singleton mode.

Namespace _ 01 Singleton mode {class Program {static void Main (string [] args) {// we access the unique instance Singleton s = Singleton through global access points. only ;}} public class Singleton // Singleton class {// 2. provides a global access point (which is actually a Global static field). The unique instance of this class can be accessed externally. // because the static field belongs to the class, therefore, only one instance is guaranteed. Public static Singleton only = new Singleton (); // 1 declare the constructor as private, this ensures that the class cannot be instantiated outside the class (that is, the class instance cannot be obtained outside the class) private Singleton (){}}}View Code

We should be able to understand the implementation code of the singleton mode.

Step 1: The class constructor is a private constructor, so that we cannot instantiate this function externally through Singleton s = new Singleton, because the members of the Private life can only be accessed within the class. This ensures that there is only one instance of the class (that is, it is instantiated within the class ).

Step 2: declare a public Global static field in the class (in fact, a global access point is provided), through which the external object can obtain the unique instance of the class. Remember, this field must be declared as a static field. If it is declared as a non-static field, it cannot be accessed outside the class. Because non-static members can only be accessed through class instances, we cannot instantiate this class outside the class.

The following is the second question. What is the role of Singleton mode? In fact, the definition of the singleton mode has given us the answer: Make an object of the Class A unique instance in the system. It seems like you didn't say it. For example, you will know what benefits he has. There can be multiple print tasks in one operating system, but we only have one printer and only one task is working at a time, in this case, the printer class in the memory must be a singleton. Otherwise, multiple print tasks can be executed simultaneously, which will obviously cause errors.

 

 

 

 

 

 

 

 

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.