. NET: A Brief Introduction to the design mode (single-piece Mode)

Source: Internet
Author: User


Today we will talk about the single-piece mode. In fact, the single-piece mode is a simple design mode, which is often used in our daily development process.


Single-piece mode: the single-piece mode is a programming method used to ensure that there is only one class instance in the entire application and the resources occupied by this instance are shared throughout the application.


I think when beginners are familiar with some basic concepts, the single-piece mode is very simple, because beginners are not very familiar with some concepts. Let's first remove these obstacles that block our understanding.


Most beginners cannot figure out the differences between "static objects" and "instance objects. Of course, everyone has a transitional period. Don't hurry up. As long as we study calmly, we are on the way to success.


[Any design pattern requires us to master the idea of basic OOP] [Wang qingpei has all rights reserved. Please give a signature for reprinting]


Only after we have mastered the basic syntax, concepts, and ideas of object-oriented language can we discuss, study, and write the design patterns. If we still have problems using object-oriented languages, I am afraid we will not be able to learn the design model any more. Of course, no one can guarantee that they can be proficient in all the syntaxes of a language. However, we must maintain a positive learning attitude, and we can always learn from each other over time.


Obstacle: differences between static and non-static classes


Static classes are static classes in the context of the program and do not change the address with garbage collection or stack adjustment. So the instance object can access static objects at any time. The initialization of the static class is uncertain. It is generally executed when the object of the static class is called for the first time.


The so-called non-static class: the class is just a definition and needs to be instantiated as a template to produce a specific instance object. This instance object is the space in the memory. The address of the Instance Object is unknown, so the static object cannot directly access the non-static instance object. [Wang qingpei has all rights reserved. For more information, please sign it.]


I think the above two sentences may not help beginners to understand them. We will draw a picture to compare the image.





Member is an instance class without static keyword modification. This raises two questions: 1. Why can a static object be contained in an instance class? 2. Why cannot I include instance objects in a static class?


In fact, there is no conflict between the two. Let's think that the instance class can be instantiated. When we want to use it, we will create a NEW one, but the static class is static in the program, once initialized, the memory location is determined. Therefore, no matter how many uncertain locations the instance object has, the location of the static object is determined, the instance programmer can access static objects everywhere.


However, in turn, static objects cannot contain instance members. Because the instance members are not sure about their locations, the static class cannot find the memory address of the Instance Object. You may also ask why I can define two types of objects, one is an instance Member and the other is a static member in the instance class. We all know that there cannot be two classes with the same name in the program. In fact, we can divide a class into two classes, one static class and one non-static class. This can also achieve the same effect, but it does not seem to meet our daily object-oriented development. In daily life, this is the case. For example, I have a template that can be used to import gypsum into this template to form a certain model entity. gypsum and entity are originally the relationship between classes and instances, however, some objects are the information shared by each plaster. For example, each entity has a template ID, so each entity must reference this template. With this reference number, you can find the template. [Wang qingpei has all rights reserved. For more information, please sign it.]





Although it is a Member class, we use two objects, both of which are dynamically separated from Member. As long as we understand this problem, it is much easier to understand the single-piece mode.


Next we will go to the single-piece mode for learning.


In fact, the single-piece mode is solved by means of mixed classes, and only one instance of the class should be guaranteed. Let's explore along the way. Every instance class can instantiate a specific object by calling the constructor. Isn't it possible to expose the constructor of this instance class? Of course, it cannot be made public. We need to use a static member to keep the reference of the Instance Object throughout its lifecycle. Next time who needs this instance object, we will read the reference from the static member, you can get the unique instance;







View sourceprint? Using System;


Using System. Collections. Generic;


Using System. Text;



Namespace ConsoleApplication1




Public class Member




/// <Summary>


/// Set the default constructor to private


/// </Summary>


Private Member (){}


/// <Summary>


/// Save the reference to the Member instance


/// </Summary>


Private static Member slignmemer;


/// <Summary>


/// Unified method for obtaining a Member object


/// </Summary>


/// <Returns> </returns>


Public static Member GetMember ()




If (slignmemer = null)




Slignmemer = new Member ();


Return slignmemer;




Return slignmemer;




Public int age;


Public int sex;


Public int scode;






Summary: The single-piece mode is completely explained. In fact, the single-piece mode is the use of mixed classes. As long as we are familiar with the use of classes, we can understand the single-piece 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.