Design Patterns I - Singleton, Double-Checked locking, Lazy Initialization

/*

Author: Jiangong SUN

*/

Last update: 29/05/2013, 30/05/2013


There are 4 categories of design patterns called Gang of Four (GoF):

- Creational patterns

- Structural patterns

- Behavior patterns

- Concurrency patterns


Singleton is a creational pattern. 

Double-checked locking is a concurrency pattern.

Lazy initialization is a a creational pattern.


Singleton is a design pattern used to limit one class to have just only 1 instance.


Here is an example of basic Singleton:

Design Patterns I - Singleton, Double-Checked locking, Lazy Initialization_第1张图片



Here is an example about Double-check locking singleton:

volatile: ensures that one thread retrieves the most up-to-date value written by another thread.


Here is another Lazy initialization singleton example:

Design Patterns I - Singleton, Double-Checked locking, Lazy Initialization_第2张图片


Call the methods inside the singleton classes.


Design Patterns I - Singleton, Double-Checked locking, Lazy Initialization_第3张图片


The two implementation of singleton works, and you can choose the ideal one with your special requirements. 


I hope this post does help to you. Enjoy coding!


Refrence: 

http://csharpindepth.com/Articles/General/Singleton.aspx

http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list

你可能感兴趣的:(Singleton,Pattern,design,csharp,Double-checked,Locki)