[Design mode sorting Note 8] Singleton)

Source: Internet
Author: User

[Guide]

[Design mode sorting Note 1] basic knowledge

[Design mode sorting Note 2] simple factory Mode)

[Design mode sorting Note 3] factory Mode)

[Design pattern sorting Note 4] abstract factory pattern (Abstract Factory)

[Design pattern sorting Note 5] creator pattern (builder)

[Design pattern arrangement Note 6] Summary of factory pattern and creator Pattern

[Design mode sorting Note 7] prototype)

[Design mode sorting Note 8] Singleton)

[Design mode sorting Note 9] appearance mode (facade)

... Later, including some examples

[/Guide]

 

 

The meaning of the singleton mode can be understood literally, that is, a class has only one instance. There are two types: hungry and lazy. How can we understand the hunger style? That is, when the class is initialized, it creates its own object. A typical understanding is that a person is very hungry and eats something at the beginning. The lazy way is to create an object only when needed. A typical understanding is that a person is very lazy and will not do anything first, but will do things only when necessary.

The following describes the reality of the singleton mode:

Define a class:

Code

Using System;

NamespaceConsoleapp
{
Public ClassSubcompany
{
PublicSubcompany ()
{

}

Public StaticSubcompany instance
{

Get
{
If (Instance =   Null )
{
Instance =   New Subcompany ();
}
Return Instance;
}
}

Private StaticSubcompany instance= Null;

///   <Summary>
/// Return Group Company Name
///   </Summary>
///   <Returns> </returns>
Public   String Getheadname
{
Get
{
Return   " China Mobile " ;
}
}
}
}

 

The call is as follows:

Code

Using System;

Namespace Consoleapp
{
Class Program
{
Public   Static   Void Main ( String [] ARGs)
{
String Headname = Subcompany. instance. getheadname;
Console. writeline (headname );

Console. Readline ();
}
}
}

 

 

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.