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 }
  • 相关阅读:
    MinGW GCC 7.1.0 2017年6月份出炉啦
    java面试题-框架篇九
    spring-AOP原理
    spring的bean管理(注解)
    23种设计模式(1)-单例模式
    SSH框架面试题集锦
    JQuery基础
    实现用户注册
    spring与hibernate的整合
    spring-IOC理解1
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1715077.html
Copyright © 2011-2022 走看看