Skip to content

Commit 24092d2

Browse files
committed
add singleton pattern
1 parent 8c1bd3a commit 24092d2

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

Creational/Singleton/Program.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace Singleton {
5+
6+
public class DoublecheckSingleton {
7+
8+
private volatile static DoublecheckSingleton _instance;
9+
10+
private DoublecheckSingleton() {
11+
}
12+
13+
public static DoublecheckSingleton Instance {
14+
get {
15+
if (_instance == null) {
16+
lock (typeof(DoublecheckSingleton)) {
17+
if (_instance == null) {
18+
Console.WriteLine("Initialize a double check singleton.");
19+
_instance = new DoublecheckSingleton();
20+
}
21+
}
22+
}
23+
return _instance;
24+
}
25+
}
26+
}
27+
28+
public class LazySingleton {
29+
30+
private static readonly Lazy<LazySingleton> LazyInstance = new Lazy<LazySingleton>(() => {
31+
Console.WriteLine("Initialize a lazy singleton.");
32+
return new LazySingleton();
33+
});
34+
35+
private LazySingleton() {
36+
}
37+
38+
public static LazySingleton Instance {
39+
get {
40+
return LazyInstance.Value;
41+
}
42+
}
43+
}
44+
45+
class Program {
46+
47+
static void Main(string[] args) {
48+
49+
for (int i = 0; i < 100; i++) {
50+
Task.Factory.StartNew(() => Console.WriteLine(DoublecheckSingleton.Instance.ToString()));
51+
}
52+
53+
for (int i = 0; i < 100; i++) {
54+
Task.Factory.StartNew(() => Console.WriteLine(LazySingleton.Instance.ToString()));
55+
}
56+
57+
Console.ReadKey();
58+
}
59+
}
60+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Singleton")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Singleton")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("d14f4089-6605-4004-8400-853b36efe937")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Creational/Singleton/Singleton.csproj

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{14333E87-2DCC-42A7-91FF-2DFEABF2AA95}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Singleton</RootNamespace>
12+
<AssemblyName>Singleton</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
50+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
51+
Other similar extension points exist, see Microsoft.Common.targets.
52+
<Target Name="BeforeBuild">
53+
</Target>
54+
<Target Name="AfterBuild">
55+
</Target>
56+
-->
57+
</Project>

DesignPatterns.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryMethod", "Creational
3535
EndProject
3636
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Creational\Prototype\Prototype.csproj", "{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}"
3737
EndProject
38+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Singleton", "Creational\Singleton\Singleton.csproj", "{14333E87-2DCC-42A7-91FF-2DFEABF2AA95}"
39+
EndProject
3840
Global
3941
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4042
Debug|Any CPU = Debug|Any CPU
@@ -135,6 +137,12 @@ Global
135137
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Release|Any CPU.ActiveCfg = Release|x86
136138
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Release|x86.ActiveCfg = Release|x86
137139
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Release|x86.Build.0 = Release|x86
140+
{14333E87-2DCC-42A7-91FF-2DFEABF2AA95}.Debug|Any CPU.ActiveCfg = Debug|x86
141+
{14333E87-2DCC-42A7-91FF-2DFEABF2AA95}.Debug|x86.ActiveCfg = Debug|x86
142+
{14333E87-2DCC-42A7-91FF-2DFEABF2AA95}.Debug|x86.Build.0 = Debug|x86
143+
{14333E87-2DCC-42A7-91FF-2DFEABF2AA95}.Release|Any CPU.ActiveCfg = Release|x86
144+
{14333E87-2DCC-42A7-91FF-2DFEABF2AA95}.Release|x86.ActiveCfg = Release|x86
145+
{14333E87-2DCC-42A7-91FF-2DFEABF2AA95}.Release|x86.Build.0 = Release|x86
138146
EndGlobalSection
139147
GlobalSection(SolutionProperties) = preSolution
140148
HideSolutionNode = FALSE
@@ -155,5 +163,6 @@ Global
155163
{92346BF9-D8E8-4D1E-A3B6-48EA49BD9ADC} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C}
156164
{040AFA71-3175-4C6C-8DB2-98810D3E98F4} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C}
157165
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C}
166+
{14333E87-2DCC-42A7-91FF-2DFEABF2AA95} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C}
158167
EndGlobalSection
159168
EndGlobal

0 commit comments

Comments
 (0)