zoukankan      html  css  js  c++  java
  • 浅尝DesignPattern_Singleton

     Singleton

    -------------------------------------------------------------------------------------------------------

     UML:

    --------------------------------------------------------------------------------------------------------

    SAMPLE:

    1 class SingletonClient
    2 {
    3 static void Main(string[] args)
    4 {
    5 Singleton singleton = Singleton.getInstance();
    6 singleton.SaySomething();
    7
    8 // Wait for user
    9   Console.ReadKey();
    10 }
    11 }
    12
    13 #region Singleton
    14
    15 public class Singleton
    16 {
    17 private volatile static Singleton _uniqueInstance;
    18 private static readonly object _syncLock = new object();
    19
    20 private Singleton() {}
    21
    22 public static Singleton getInstance()
    23 {
    24 if (_uniqueInstance == null)
    25 {
    26 // Lock area where instance is created
    27   lock(_syncLock)
    28 {
    29 if (_uniqueInstance == null)
    30 {
    31 _uniqueInstance = new Singleton();
    32 }
    33 }
    34 }
    35 return _uniqueInstance;
    36 }
    37
    38 public void SaySomething()
    39 {
    40 Console.WriteLine("I am double checked, therefore I am");
    41 }
    42 }
  • 相关阅读:
    【转】5亿个数找中位数
    C++二维数组名的再探索
    转载 图像卷积
    PowerDesigner的使用一
    Spring注解详解
    JSP页面以及简单的指令
    Javascript学习总结
    html第一天
    Chrome开发,debug的使用方法。
    SVN使用
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1715077.html
Copyright © 2011-2022 走看看