├── .gitignore ├── Metigator.DesignPatterns.sln ├── Singleton.NotThreadSafe ├── LogMessage.cs ├── LogType.cs ├── MemoryLogger.cs ├── Program.cs └── Singleton.NotThreadSafe.csproj ├── Singleton.SingletonEagerLoading ├── LogMessage.cs ├── LogType.cs ├── MemoryLogger.cs ├── Program.cs └── Singleton.SingletonEagerLoading.csproj ├── Singleton.SingletonLazyLoading ├── LogMessage.cs ├── LogType.cs ├── MemoryLogger.cs ├── Program.cs └── Singleton.SingletonLazyLoading.csproj ├── Singleton.Start ├── LogMessage.cs ├── LogType.cs ├── MemoryLogger.cs ├── Program.cs └── Singleton.Start.csproj ├── Singleton.ThreadSafeUsingDoubkeCheckedLock ├── LogMessage.cs ├── LogType.cs ├── MemoryLogger.cs ├── Program.cs └── Singleton.ThreadSafeUsingDoubkeCheckedLock.csproj └── Singleton.ThreadSafeUsingLock ├── LogMessage.cs ├── LogType.cs ├── MemoryLogger.cs ├── Program.cs └── Singleton.ThreadSafeUsingLock.csproj /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.tlog 97 | *.vspscc 98 | *.vssscc 99 | .builds 100 | *.pidb 101 | *.svclog 102 | *.scc 103 | 104 | # Chutzpah Test files 105 | _Chutzpah* 106 | 107 | # Visual C++ cache files 108 | ipch/ 109 | *.aps 110 | *.ncb 111 | *.opendb 112 | *.opensdf 113 | *.sdf 114 | *.cachefile 115 | *.VC.db 116 | *.VC.VC.opendb 117 | 118 | # Visual Studio profiler 119 | *.psess 120 | *.vsp 121 | *.vspx 122 | *.sap 123 | 124 | # Visual Studio Trace Files 125 | *.e2e 126 | 127 | # TFS 2012 Local Workspace 128 | $tf/ 129 | 130 | # Guidance Automation Toolkit 131 | *.gpState 132 | 133 | # ReSharper is a .NET coding add-in 134 | _ReSharper*/ 135 | *.[Rr]e[Ss]harper 136 | *.DotSettings.user 137 | 138 | # TeamCity is a build add-in 139 | _TeamCity* 140 | 141 | # DotCover is a Code Coverage Tool 142 | *.dotCover 143 | 144 | # AxoCover is a Code Coverage Tool 145 | .axoCover/* 146 | !.axoCover/settings.json 147 | 148 | # Coverlet is a free, cross platform Code Coverage Tool 149 | coverage*.json 150 | coverage*.xml 151 | coverage*.info 152 | 153 | # Visual Studio code coverage results 154 | *.coverage 155 | *.coveragexml 156 | 157 | # NCrunch 158 | _NCrunch_* 159 | .*crunch*.local.xml 160 | nCrunchTemp_* 161 | 162 | # MightyMoose 163 | *.mm.* 164 | AutoTest.Net/ 165 | 166 | # Web workbench (sass) 167 | .sass-cache/ 168 | 169 | # Installshield output folder 170 | [Ee]xpress/ 171 | 172 | # DocProject is a documentation generator add-in 173 | DocProject/buildhelp/ 174 | DocProject/Help/*.HxT 175 | DocProject/Help/*.HxC 176 | DocProject/Help/*.hhc 177 | DocProject/Help/*.hhk 178 | DocProject/Help/*.hhp 179 | DocProject/Help/Html2 180 | DocProject/Help/html 181 | 182 | # Click-Once directory 183 | publish/ 184 | 185 | # Publish Web Output 186 | *.[Pp]ublish.xml 187 | *.azurePubxml 188 | # Note: Comment the next line if you want to checkin your web deploy settings, 189 | # but database connection strings (with potential passwords) will be unencrypted 190 | *.pubxml 191 | *.publishproj 192 | 193 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 194 | # checkin your Azure Web App publish settings, but sensitive information contained 195 | # in these scripts will be unencrypted 196 | PublishScripts/ 197 | 198 | # NuGet Packages 199 | *.nupkg 200 | # NuGet Symbol Packages 201 | *.snupkg 202 | # The packages folder can be ignored because of Package Restore 203 | **/[Pp]ackages/* 204 | # except build/, which is used as an MSBuild target. 205 | !**/[Pp]ackages/build/ 206 | # Uncomment if necessary however generally it will be regenerated when needed 207 | #!**/[Pp]ackages/repositories.config 208 | # NuGet v3's project.json files produces more ignorable files 209 | *.nuget.props 210 | *.nuget.targets 211 | 212 | # Microsoft Azure Build Output 213 | csx/ 214 | *.build.csdef 215 | 216 | # Microsoft Azure Emulator 217 | ecf/ 218 | rcf/ 219 | 220 | # Windows Store app package directories and files 221 | AppPackages/ 222 | BundleArtifacts/ 223 | Package.StoreAssociation.xml 224 | _pkginfo.txt 225 | *.appx 226 | *.appxbundle 227 | *.appxupload 228 | 229 | # Visual Studio cache files 230 | # files ending in .cache can be ignored 231 | *.[Cc]ache 232 | # but keep track of directories ending in .cache 233 | !?*.[Cc]ache/ 234 | 235 | # Others 236 | ClientBin/ 237 | ~$* 238 | *~ 239 | *.dbmdl 240 | *.dbproj.schemaview 241 | *.jfm 242 | *.pfx 243 | *.publishsettings 244 | orleans.codegen.cs 245 | 246 | # Including strong name files can present a security risk 247 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 248 | #*.snk 249 | 250 | # Since there are multiple workflows, uncomment next line to ignore bower_components 251 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 252 | #bower_components/ 253 | 254 | # RIA/Silverlight projects 255 | Generated_Code/ 256 | 257 | # Backup & report files from converting an old project file 258 | # to a newer Visual Studio version. Backup files are not needed, 259 | # because we have git ;-) 260 | _UpgradeReport_Files/ 261 | Backup*/ 262 | UpgradeLog*.XML 263 | UpgradeLog*.htm 264 | ServiceFabricBackup/ 265 | *.rptproj.bak 266 | 267 | # SQL Server files 268 | *.mdf 269 | *.ldf 270 | *.ndf 271 | 272 | # Business Intelligence projects 273 | *.rdl.data 274 | *.bim.layout 275 | *.bim_*.settings 276 | *.rptproj.rsuser 277 | *- [Bb]ackup.rdl 278 | *- [Bb]ackup ([0-9]).rdl 279 | *- [Bb]ackup ([0-9][0-9]).rdl 280 | 281 | # Microsoft Fakes 282 | FakesAssemblies/ 283 | 284 | # GhostDoc plugin setting file 285 | *.GhostDoc.xml 286 | 287 | # Node.js Tools for Visual Studio 288 | .ntvs_analysis.dat 289 | node_modules/ 290 | 291 | # Visual Studio 6 build log 292 | *.plg 293 | 294 | # Visual Studio 6 workspace options file 295 | *.opt 296 | 297 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 298 | *.vbw 299 | 300 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 301 | *.vbp 302 | 303 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 304 | *.dsw 305 | *.dsp 306 | 307 | # Visual Studio 6 technical files 308 | *.ncb 309 | *.aps 310 | 311 | # Visual Studio LightSwitch build output 312 | **/*.HTMLClient/GeneratedArtifacts 313 | **/*.DesktopClient/GeneratedArtifacts 314 | **/*.DesktopClient/ModelManifest.xml 315 | **/*.Server/GeneratedArtifacts 316 | **/*.Server/ModelManifest.xml 317 | _Pvt_Extensions 318 | 319 | # Paket dependency manager 320 | .paket/paket.exe 321 | paket-files/ 322 | 323 | # FAKE - F# Make 324 | .fake/ 325 | 326 | # CodeRush personal settings 327 | .cr/personal 328 | 329 | # Python Tools for Visual Studio (PTVS) 330 | __pycache__/ 331 | *.pyc 332 | 333 | # Cake - Uncomment if you are using it 334 | # tools/** 335 | # !tools/packages.config 336 | 337 | # Tabs Studio 338 | *.tss 339 | 340 | # Telerik's JustMock configuration file 341 | *.jmconfig 342 | 343 | # BizTalk build output 344 | *.btp.cs 345 | *.btm.cs 346 | *.odx.cs 347 | *.xsd.cs 348 | 349 | # OpenCover UI analysis results 350 | OpenCover/ 351 | 352 | # Azure Stream Analytics local run output 353 | ASALocalRun/ 354 | 355 | # MSBuild Binary and Structured Log 356 | *.binlog 357 | 358 | # NVidia Nsight GPU debugger configuration file 359 | *.nvuser 360 | 361 | # MFractors (Xamarin productivity tool) working folder 362 | .mfractor/ 363 | 364 | # Local History for Visual Studio 365 | .localhistory/ 366 | 367 | # Visual Studio History (VSHistory) files 368 | .vshistory/ 369 | 370 | # BeatPulse healthcheck temp database 371 | healthchecksdb 372 | 373 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 374 | MigrationBackup/ 375 | 376 | # Ionide (cross platform F# VS Code tools) working folder 377 | .ionide/ 378 | 379 | # Fody - auto-generated XML schema 380 | FodyWeavers.xsd 381 | 382 | # VS Code files for those working on multiple tools 383 | .vscode/* 384 | !.vscode/settings.json 385 | !.vscode/tasks.json 386 | !.vscode/launch.json 387 | !.vscode/extensions.json 388 | *.code-workspace 389 | 390 | # Local History for Visual Studio Code 391 | .history/ 392 | 393 | # Windows Installer files from build outputs 394 | *.cab 395 | *.msi 396 | *.msix 397 | *.msm 398 | *.msp 399 | 400 | # JetBrains Rider 401 | *.sln.iml 402 | 403 | ## 404 | ## Visual studio for Mac 405 | ## 406 | 407 | 408 | # globs 409 | Makefile.in 410 | *.userprefs 411 | *.usertasks 412 | config.make 413 | config.status 414 | aclocal.m4 415 | install-sh 416 | autom4te.cache/ 417 | *.tar.gz 418 | tarballs/ 419 | test-results/ 420 | 421 | # Mac bundle stuff 422 | *.dmg 423 | *.app 424 | 425 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 426 | # General 427 | .DS_Store 428 | .AppleDouble 429 | .LSOverride 430 | 431 | # Icon must end with two \r 432 | Icon 433 | 434 | 435 | # Thumbnails 436 | ._* 437 | 438 | # Files that might appear in the root of a volume 439 | .DocumentRevisions-V100 440 | .fseventsd 441 | .Spotlight-V100 442 | .TemporaryItems 443 | .Trashes 444 | .VolumeIcon.icns 445 | .com.apple.timemachine.donotpresent 446 | 447 | # Directories potentially created on remote AFP share 448 | .AppleDB 449 | .AppleDesktop 450 | Network Trash Folder 451 | Temporary Items 452 | .apdisk 453 | 454 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 455 | # Windows thumbnail cache files 456 | Thumbs.db 457 | ehthumbs.db 458 | ehthumbs_vista.db 459 | 460 | # Dump file 461 | *.stackdump 462 | 463 | # Folder config file 464 | [Dd]esktop.ini 465 | 466 | # Recycle Bin used on file shares 467 | $RECYCLE.BIN/ 468 | 469 | # Windows Installer files 470 | *.cab 471 | *.msi 472 | *.msix 473 | *.msm 474 | *.msp 475 | 476 | # Windows shortcuts 477 | *.lnk 478 | -------------------------------------------------------------------------------- /Metigator.DesignPatterns.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.3.32825.248 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton.NotThreadSafe", "Singleton.NotThreadSafe\Singleton.NotThreadSafe.csproj", "{43B8C10D-1D30-4A5F-A04D-6F0BBAF4819E}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton.Start", "Singleton.Start\Singleton.Start.csproj", "{37C3401A-177E-41BF-B039-524B6F8E374F}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton.ThreadSafeUsingLock", "Singleton.ThreadSafeUsingLock\Singleton.ThreadSafeUsingLock.csproj", "{C17B4433-9475-44C4-8DF8-510D20F33572}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton.ThreadSafeUsingDoubkeCheckedLock", "Singleton.ThreadSafeUsingDoubkeCheckedLock\Singleton.ThreadSafeUsingDoubkeCheckedLock.csproj", "{494FA6E2-ABCB-4C26-B46E-A5325A3349D6}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton.SingletonEagerLoading", "Singleton.SingletonEagerLoading\Singleton.SingletonEagerLoading.csproj", "{F59314C8-7E36-4E89-84EC-709864725263}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton.SingletonLazyLoading", "Singleton.SingletonLazyLoading\Singleton.SingletonLazyLoading.csproj", "{5A33F666-4F9C-42A7-8FE0-B7F6CBCC7A8D}" 17 | EndProject 18 | Global 19 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 20 | Debug|Any CPU = Debug|Any CPU 21 | Release|Any CPU = Release|Any CPU 22 | EndGlobalSection 23 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 24 | {43B8C10D-1D30-4A5F-A04D-6F0BBAF4819E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {43B8C10D-1D30-4A5F-A04D-6F0BBAF4819E}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {43B8C10D-1D30-4A5F-A04D-6F0BBAF4819E}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {43B8C10D-1D30-4A5F-A04D-6F0BBAF4819E}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {37C3401A-177E-41BF-B039-524B6F8E374F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {37C3401A-177E-41BF-B039-524B6F8E374F}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {37C3401A-177E-41BF-B039-524B6F8E374F}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {37C3401A-177E-41BF-B039-524B6F8E374F}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {C17B4433-9475-44C4-8DF8-510D20F33572}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {C17B4433-9475-44C4-8DF8-510D20F33572}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {C17B4433-9475-44C4-8DF8-510D20F33572}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {C17B4433-9475-44C4-8DF8-510D20F33572}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {494FA6E2-ABCB-4C26-B46E-A5325A3349D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {494FA6E2-ABCB-4C26-B46E-A5325A3349D6}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {494FA6E2-ABCB-4C26-B46E-A5325A3349D6}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {494FA6E2-ABCB-4C26-B46E-A5325A3349D6}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {F59314C8-7E36-4E89-84EC-709864725263}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {F59314C8-7E36-4E89-84EC-709864725263}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {F59314C8-7E36-4E89-84EC-709864725263}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {F59314C8-7E36-4E89-84EC-709864725263}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {5A33F666-4F9C-42A7-8FE0-B7F6CBCC7A8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {5A33F666-4F9C-42A7-8FE0-B7F6CBCC7A8D}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {5A33F666-4F9C-42A7-8FE0-B7F6CBCC7A8D}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {5A33F666-4F9C-42A7-8FE0-B7F6CBCC7A8D}.Release|Any CPU.Build.0 = Release|Any CPU 48 | EndGlobalSection 49 | GlobalSection(SolutionProperties) = preSolution 50 | HideSolutionNode = FALSE 51 | EndGlobalSection 52 | GlobalSection(ExtensibilityGlobals) = postSolution 53 | SolutionGuid = {5DA2B6A2-458A-467C-8545-65FFA09C0E6E} 54 | EndGlobalSection 55 | EndGlobal 56 | -------------------------------------------------------------------------------- /Singleton.NotThreadSafe/LogMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Singleton.NotThreadSafe 4 | { 5 | public class LogMessage 6 | { 7 | public string Message { get; set; } 8 | public LogType LogType { get; set; } 9 | public DateTime CreatedAt { get; set; } 10 | 11 | public override string ToString() 12 | { 13 | var timestamp = CreatedAt.ToString("yyyy-MM-dd hh:mm"); 14 | 15 | return $"{LogType.ToString().PadLeft(7, ' ')} [{timestamp}] {Message}"; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Singleton.NotThreadSafe/LogType.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton.NotThreadSafe 2 | { 3 | public enum LogType 4 | { 5 | INFO, 6 | WARNING, 7 | ERROR 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Singleton.NotThreadSafe/MemoryLogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | 5 | namespace Singleton.NotThreadSafe 6 | { 7 | public class MemoryLogger 8 | { 9 | private int _InfoCount; 10 | private int _WarningCount; 11 | private int _ErrorCount; 12 | 13 | private static MemoryLogger _instance = null; 14 | 15 | private List _logs = new List(); 16 | public IReadOnlyCollection Logs => _logs; 17 | 18 | private MemoryLogger() {} 19 | 20 | public static MemoryLogger GetLogger 21 | { 22 | get 23 | { 24 | if (_instance == null) 25 | { 26 | _instance = new MemoryLogger(); 27 | } 28 | return _instance; 29 | } 30 | } 31 | private void Log(string message, LogType logType) 32 | { 33 | _logs.Add(new LogMessage 34 | { 35 | Message = message, 36 | LogType = logType, 37 | CreatedAt = DateTime.Now 38 | }); 39 | } 40 | 41 | public void LogInfo(string message) 42 | { 43 | ++_InfoCount; 44 | Log(message, LogType.INFO); 45 | } 46 | public void LogError(string message) 47 | { 48 | ++_ErrorCount; 49 | Log(message, LogType.ERROR); 50 | } 51 | public void LogWarning(string message) 52 | { 53 | ++_WarningCount; 54 | Log(message, LogType.WARNING); 55 | } 56 | 57 | public void ShowLog() 58 | { 59 | 60 | _logs.ForEach(x => Console.WriteLine(x)); 61 | Console.WriteLine($"-------------------------------"); 62 | 63 | Console.WriteLine($"Info ({_InfoCount}), Warning ({_WarningCount}), Error ({_ErrorCount})"); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Singleton.NotThreadSafe/Program.cs: -------------------------------------------------------------------------------- 1 | using Singleton.NotThreadSafe; 2 | using System; 3 | 4 | class Program 5 | { 6 | static MemoryLogger logger; 7 | 8 | static void Main(string[] args) 9 | { 10 | AssignVoucher("issam@metigator.com", "ABC123"); 11 | 12 | UseVoucher("ABC123"); 13 | 14 | logger.ShowLog(); 15 | 16 | Console.ReadKey(); 17 | } 18 | 19 | static void AssignVoucher(string email, string voucher) 20 | { 21 | logger = MemoryLogger.GetLogger; 22 | // Logic here 23 | logger.LogInfo($"Voucher '{voucher}' assigned"); 24 | 25 | // another logic 26 | logger.LogError($"unable to send email '{email}'"); 27 | } 28 | static void UseVoucher(string voucher) 29 | { 30 | logger = MemoryLogger.GetLogger; 31 | // Logic here 32 | logger.LogWarning($"3 attempts made to validate the voucher"); 33 | 34 | // Logic here 35 | logger.LogInfo($"'{voucher}' is used"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Singleton.NotThreadSafe/Singleton.NotThreadSafe.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Singleton.SingletonEagerLoading/LogMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Singleton.SingletonEagerLoading 4 | { 5 | public class LogMessage 6 | { 7 | public string Message { get; set; } 8 | public LogType LogType { get; set; } 9 | public DateTime CreatedAt { get; set; } 10 | 11 | public override string ToString() 12 | { 13 | var timestamp = CreatedAt.ToString("yyyy-MM-dd hh:mm"); 14 | 15 | return $"{LogType.ToString().PadLeft(7, ' ')} [{timestamp}] {Message}"; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Singleton.SingletonEagerLoading/LogType.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton.SingletonEagerLoading 2 | { 3 | public enum LogType 4 | { 5 | INFO, 6 | WARNING, 7 | ERROR 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Singleton.SingletonEagerLoading/MemoryLogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Singleton.SingletonEagerLoading 5 | { 6 | public class MemoryLogger 7 | { 8 | private static readonly MemoryLogger _instance = new MemoryLogger(); 9 | 10 | private int _InfoCount; 11 | private int _WarningCount; 12 | private int _ErrorCount; 13 | 14 | private MemoryLogger() { } 15 | 16 | 17 | // T1, T2 18 | public static MemoryLogger GetLogger 19 | { 20 | get 21 | { 22 | return _instance; 23 | } 24 | } 25 | 26 | private List _logs = new List(); 27 | public IReadOnlyCollection Logs => _logs; 28 | 29 | 30 | private void Log(string message, LogType logType) 31 | { 32 | _logs.Add(new LogMessage 33 | { 34 | Message = message, 35 | LogType = logType, 36 | CreatedAt = DateTime.Now 37 | }); 38 | } 39 | 40 | public void LogInfo(string message) 41 | { 42 | ++_InfoCount; 43 | Log(message, LogType.INFO); 44 | } 45 | public void LogError(string message) 46 | { 47 | ++_ErrorCount; 48 | Log(message, LogType.ERROR); 49 | } 50 | public void LogWarning(string message) 51 | { 52 | ++_WarningCount; 53 | Log(message, LogType.WARNING); 54 | } 55 | 56 | public void ShowLog() 57 | { 58 | 59 | _logs.ForEach(x => Console.WriteLine(x)); 60 | Console.WriteLine($"-------------------------------"); 61 | 62 | Console.WriteLine($"Info ({_InfoCount}), Warning ({_WarningCount}), Error ({_ErrorCount})"); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Singleton.SingletonEagerLoading/Program.cs: -------------------------------------------------------------------------------- 1 | using Singleton.SingletonEagerLoading; 2 | using System; 3 | 4 | class Program 5 | { 6 | static MemoryLogger logger; 7 | 8 | static void Main(string[] args) 9 | { 10 | AssignVoucher("issam@metigator.com", "ABC123"); 11 | 12 | UseVoucher("ABC123"); 13 | 14 | logger.ShowLog(); 15 | 16 | Console.ReadKey(); 17 | } 18 | 19 | static void AssignVoucher(string email, string voucher) 20 | { 21 | logger = MemoryLogger.GetLogger; 22 | // Logic here 23 | logger.LogInfo($"Voucher '{voucher}' assigned"); 24 | 25 | // another logic 26 | logger.LogError($"unable to send email '{email}'"); 27 | } 28 | static void UseVoucher(string voucher) 29 | { 30 | logger = MemoryLogger.GetLogger; 31 | 32 | // Logic here 33 | logger.LogWarning($"3 attempts made to validate the voucher"); 34 | 35 | // Logic here 36 | logger.LogInfo($"'{voucher}' is used"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Singleton.SingletonEagerLoading/Singleton.SingletonEagerLoading.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Singleton.SingletonLazyLoading/LogMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Singleton.SingletonLazyLoading 4 | { 5 | public class LogMessage 6 | { 7 | public string Message { get; set; } 8 | public LogType LogType { get; set; } 9 | public DateTime CreatedAt { get; set; } 10 | 11 | public override string ToString() 12 | { 13 | var timestamp = CreatedAt.ToString("yyyy-MM-dd hh:mm"); 14 | 15 | return $"{LogType.ToString().PadLeft(7, ' ')} [{timestamp}] {Message}"; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Singleton.SingletonLazyLoading/LogType.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton.SingletonLazyLoading 2 | { 3 | public enum LogType 4 | { 5 | INFO, 6 | WARNING, 7 | ERROR 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Singleton.SingletonLazyLoading/MemoryLogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Singleton.SingletonLazyLoading 5 | { 6 | public class MemoryLogger 7 | { 8 | private static readonly Lazy _instance = 9 | new Lazy (() => new MemoryLogger ()); 10 | 11 | 12 | private int _InfoCount; 13 | private int _WarningCount; 14 | private int _ErrorCount; 15 | 16 | private MemoryLogger() { } 17 | 18 | 19 | // T1, T2 20 | public static MemoryLogger GetLogger 21 | { 22 | get 23 | { 24 | return _instance.Value; 25 | } 26 | } 27 | 28 | private List _logs = new List(); 29 | public IReadOnlyCollection Logs => _logs; 30 | 31 | 32 | private void Log(string message, LogType logType) 33 | { 34 | _logs.Add(new LogMessage 35 | { 36 | Message = message, 37 | LogType = logType, 38 | CreatedAt = DateTime.Now 39 | }); 40 | } 41 | 42 | public void LogInfo(string message) 43 | { 44 | ++_InfoCount; 45 | Log(message, LogType.INFO); 46 | } 47 | public void LogError(string message) 48 | { 49 | ++_ErrorCount; 50 | Log(message, LogType.ERROR); 51 | } 52 | public void LogWarning(string message) 53 | { 54 | ++_WarningCount; 55 | Log(message, LogType.WARNING); 56 | } 57 | 58 | public void ShowLog() 59 | { 60 | 61 | _logs.ForEach(x => Console.WriteLine(x)); 62 | Console.WriteLine($"-------------------------------"); 63 | 64 | Console.WriteLine($"Info ({_InfoCount}), Warning ({_WarningCount}), Error ({_ErrorCount})"); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Singleton.SingletonLazyLoading/Program.cs: -------------------------------------------------------------------------------- 1 | using Singleton.SingletonLazyLoading; 2 | using System; 3 | 4 | class Program 5 | { 6 | static MemoryLogger logger; 7 | 8 | static void Main(string[] args) 9 | { 10 | AssignVoucher("issam@metigator.com", "ABC123"); 11 | 12 | UseVoucher("ABC123"); 13 | 14 | logger.ShowLog(); 15 | 16 | Console.ReadKey(); 17 | } 18 | 19 | static void AssignVoucher(string email, string voucher) 20 | { 21 | logger = MemoryLogger.GetLogger; 22 | // Logic here 23 | logger.LogInfo($"Voucher '{voucher}' assigned"); 24 | 25 | // another logic 26 | logger.LogError($"unable to send email '{email}'"); 27 | } 28 | static void UseVoucher(string voucher) 29 | { 30 | logger = MemoryLogger.GetLogger; 31 | 32 | // Logic here 33 | logger.LogWarning($"3 attempts made to validate the voucher"); 34 | 35 | // Logic here 36 | logger.LogInfo($"'{voucher}' is used"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Singleton.SingletonLazyLoading/Singleton.SingletonLazyLoading.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Singleton.Start/LogMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Singleton.Start 4 | { 5 | public class LogMessage 6 | { 7 | public string Message { get; set; } 8 | public LogType LogType { get; set; } 9 | public DateTime CreatedAt { get; set; } 10 | 11 | public override string ToString() 12 | { 13 | var timestamp = CreatedAt.ToString("yyyy-MM-dd hh:mm"); 14 | 15 | return $"{LogType.ToString().PadLeft(7, ' ')} [{timestamp}] {Message}"; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Singleton.Start/LogType.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton.Start 2 | { 3 | public enum LogType 4 | { 5 | INFO, 6 | WARNING, 7 | ERROR 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Singleton.Start/MemoryLogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Singleton.Start 5 | { 6 | public class MemoryLogger 7 | { 8 | private int _InfoCount; 9 | private int _WarningCount; 10 | private int _ErrorCount; 11 | 12 | private List _logs = new List(); 13 | public IReadOnlyCollection Logs => _logs; 14 | 15 | private void Log(string message, LogType logType) 16 | { 17 | _logs.Add(new LogMessage 18 | { 19 | Message = message, 20 | LogType = logType, 21 | CreatedAt = DateTime.Now 22 | }); 23 | } 24 | 25 | public void LogInfo(string message) 26 | { 27 | ++_InfoCount; 28 | Log(message, LogType.INFO); 29 | } 30 | public void LogError(string message) 31 | { 32 | ++_ErrorCount; 33 | Log(message, LogType.ERROR); 34 | } 35 | public void LogWarning(string message) 36 | { 37 | ++_WarningCount; 38 | Log(message, LogType.WARNING); 39 | } 40 | 41 | public void ShowLog() 42 | { 43 | 44 | _logs.ForEach(x => Console.WriteLine(x)); 45 | Console.WriteLine($"-------------------------------"); 46 | 47 | Console.WriteLine($"Info ({_InfoCount}), Warning ({_WarningCount}), Error ({_ErrorCount})"); 48 | } 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Singleton.Start/Program.cs: -------------------------------------------------------------------------------- 1 | using Singleton.Start; 2 | using System; 3 | 4 | class Program 5 | { 6 | static MemoryLogger logger; 7 | 8 | static void Main(string[] args) 9 | { 10 | AssignVoucher("issam@metigator.com", "ABC123"); 11 | 12 | UseVoucher("ABC123"); 13 | 14 | logger.ShowLog(); 15 | 16 | Console.ReadKey(); 17 | } 18 | 19 | static void AssignVoucher(string email, string voucher ) 20 | { 21 | logger = new MemoryLogger(); 22 | 23 | // Logic here 24 | logger.LogInfo($"Voucher '{voucher}' assigned"); 25 | 26 | // another logic 27 | logger.LogError($"unable to send email '{email}'"); 28 | } 29 | static void UseVoucher(string voucher) 30 | { 31 | logger = new MemoryLogger(); 32 | 33 | // Logic here 34 | logger.LogWarning($"3 attempts made to validate the voucher"); 35 | 36 | // Logic here 37 | logger.LogInfo($"'{voucher}' is used"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Singleton.Start/Singleton.Start.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Singleton.ThreadSafeUsingDoubkeCheckedLock/LogMessage.cs: -------------------------------------------------------------------------------- 1 | using Singleton.ThreadSafeUsingDoubkeCheckedLock; 2 | using System; 3 | 4 | namespace Singleton.ThreadSafeUsingLock 5 | { 6 | public class LogMessage 7 | { 8 | public string Message { get; set; } 9 | public LogType LogType { get; set; } 10 | public DateTime CreatedAt { get; set; } 11 | 12 | public override string ToString() 13 | { 14 | var timestamp = CreatedAt.ToString("yyyy-MM-dd hh:mm"); 15 | 16 | return $"{LogType.ToString().PadLeft(7, ' ')} [{timestamp}] {Message}"; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Singleton.ThreadSafeUsingDoubkeCheckedLock/LogType.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton.ThreadSafeUsingDoubkeCheckedLock 2 | { 3 | public enum LogType 4 | { 5 | INFO, 6 | WARNING, 7 | ERROR 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Singleton.ThreadSafeUsingDoubkeCheckedLock/MemoryLogger.cs: -------------------------------------------------------------------------------- 1 | using Singleton.ThreadSafeUsingDoubkeCheckedLock; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace Singleton.ThreadSafeUsingLock 6 | { 7 | public class MemoryLogger 8 | { 9 | private static MemoryLogger _instance = null; 10 | private static readonly object _lock = new object(); 11 | 12 | private int _InfoCount; 13 | private int _WarningCount; 14 | private int _ErrorCount; 15 | 16 | private MemoryLogger() { } 17 | 18 | 19 | // T1, T2 20 | public static MemoryLogger GetLogger 21 | { 22 | get 23 | { 24 | if (_instance == null) 25 | { 26 | // T1, T2 27 | lock (_lock) 28 | { 29 | if (_instance == null) 30 | { 31 | _instance = new MemoryLogger(); 32 | } 33 | } 34 | } 35 | return _instance; 36 | } 37 | } 38 | 39 | private List _logs = new List(); 40 | public IReadOnlyCollection Logs => _logs; 41 | 42 | 43 | private void Log(string message, LogType logType) 44 | { 45 | 46 | _logs.Add(new LogMessage 47 | { 48 | Message = message, 49 | LogType = logType, 50 | CreatedAt = DateTime.Now 51 | }); 52 | } 53 | 54 | public void LogInfo(string message) 55 | { 56 | ++_InfoCount; 57 | Log(message, LogType.INFO); 58 | } 59 | public void LogError(string message) 60 | { 61 | ++_ErrorCount; 62 | Log(message, LogType.ERROR); 63 | } 64 | public void LogWarning(string message) 65 | { 66 | ++_WarningCount; 67 | Log(message, LogType.WARNING); 68 | } 69 | 70 | public void ShowLog() 71 | { 72 | 73 | _logs.ForEach(x => Console.WriteLine(x)); 74 | Console.WriteLine($"-------------------------------"); 75 | 76 | Console.WriteLine($"Info ({_InfoCount}), Warning ({_WarningCount}), Error ({_ErrorCount})"); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Singleton.ThreadSafeUsingDoubkeCheckedLock/Program.cs: -------------------------------------------------------------------------------- 1 | using Singleton.ThreadSafeUsingLock; 2 | using System; 3 | 4 | class Program 5 | { 6 | static MemoryLogger logger; 7 | 8 | static void Main(string[] args) 9 | { 10 | AssignVoucher("issam@metigator.com", "ABC123"); 11 | 12 | UseVoucher("ABC123"); 13 | 14 | logger.ShowLog(); 15 | 16 | Console.ReadKey(); 17 | } 18 | 19 | static void AssignVoucher(string email, string voucher) 20 | { 21 | logger = MemoryLogger.GetLogger; 22 | // Logic here 23 | logger.LogInfo($"Voucher '{voucher}' assigned"); 24 | 25 | // another logic 26 | logger.LogError($"unable to send email '{email}'"); 27 | } 28 | static void UseVoucher(string voucher) 29 | { 30 | logger = MemoryLogger.GetLogger; 31 | 32 | // Logic here 33 | logger.LogWarning($"3 attempts made to validate the voucher"); 34 | 35 | // Logic here 36 | logger.LogInfo($"'{voucher}' is used"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Singleton.ThreadSafeUsingDoubkeCheckedLock/Singleton.ThreadSafeUsingDoubkeCheckedLock.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Singleton.ThreadSafeUsingLock/LogMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Singleton.ThreadSafeUsingLock 4 | { 5 | public class LogMessage 6 | { 7 | public string Message { get; set; } 8 | public LogType LogType { get; set; } 9 | public DateTime CreatedAt { get; set; } 10 | 11 | public override string ToString() 12 | { 13 | var timestamp = CreatedAt.ToString("yyyy-MM-dd hh:mm"); 14 | 15 | return $"{LogType.ToString().PadLeft(7, ' ')} [{timestamp}] {Message}"; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Singleton.ThreadSafeUsingLock/LogType.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton.ThreadSafeUsingLock 2 | { 3 | public enum LogType 4 | { 5 | INFO, 6 | WARNING, 7 | ERROR 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Singleton.ThreadSafeUsingLock/MemoryLogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Singleton.ThreadSafeUsingLock 5 | { 6 | public class MemoryLogger 7 | { 8 | private static MemoryLogger _instance = null; 9 | private static readonly object _lock = new object(); 10 | 11 | private int _InfoCount; 12 | private int _WarningCount; 13 | private int _ErrorCount; 14 | 15 | private MemoryLogger() { } 16 | 17 | 18 | // T1, T2 19 | public static MemoryLogger GetLogger 20 | { 21 | get 22 | { 23 | 24 | lock (_lock) 25 | { 26 | if (_instance == null) 27 | { 28 | _instance = new MemoryLogger(); 29 | } 30 | } 31 | return _instance; 32 | } 33 | } 34 | 35 | private List _logs = new List(); 36 | public IReadOnlyCollection Logs => _logs; 37 | 38 | 39 | private void Log(string message, LogType logType) 40 | { 41 | 42 | _logs.Add(new LogMessage 43 | { 44 | Message = message, 45 | LogType = logType, 46 | CreatedAt = DateTime.Now 47 | }); 48 | } 49 | 50 | public void LogInfo(string message) 51 | { 52 | ++_InfoCount; 53 | Log(message, LogType.INFO); 54 | } 55 | public void LogError(string message) 56 | { 57 | ++_ErrorCount; 58 | Log(message, LogType.ERROR); 59 | } 60 | public void LogWarning(string message) 61 | { 62 | ++_WarningCount; 63 | Log(message, LogType.WARNING); 64 | } 65 | 66 | public void ShowLog() 67 | { 68 | 69 | _logs.ForEach(x => Console.WriteLine(x)); 70 | Console.WriteLine($"-------------------------------"); 71 | 72 | Console.WriteLine($"Info ({_InfoCount}), Warning ({_WarningCount}), Error ({_ErrorCount})"); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Singleton.ThreadSafeUsingLock/Program.cs: -------------------------------------------------------------------------------- 1 | using Singleton.ThreadSafeUsingLock; 2 | using System; 3 | 4 | class Program 5 | { 6 | static MemoryLogger logger; 7 | 8 | static void Main(string[] args) 9 | { 10 | AssignVoucher("issam@metigator.com", "ABC123"); 11 | 12 | UseVoucher("ABC123"); 13 | 14 | logger.ShowLog(); 15 | 16 | Console.ReadKey(); 17 | } 18 | 19 | static void AssignVoucher(string email, string voucher) 20 | { 21 | logger = MemoryLogger.GetLogger; 22 | // Logic here 23 | logger.LogInfo($"Voucher '{voucher}' assigned"); 24 | 25 | // another logic 26 | logger.LogError($"unable to send email '{email}'"); 27 | } 28 | static void UseVoucher(string voucher) 29 | { 30 | logger = MemoryLogger.GetLogger; 31 | 32 | // Logic here 33 | logger.LogWarning($"3 attempts made to validate the voucher"); 34 | 35 | // Logic here 36 | logger.LogInfo($"'{voucher}' is used"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Singleton.ThreadSafeUsingLock/Singleton.ThreadSafeUsingLock.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | --------------------------------------------------------------------------------