[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 ();
}
}
}