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 }
  • 相关阅读:
    Codeforces 691A Fashion in Berland
    HDU 5741 Helter Skelter
    HDU 5735 Born Slippy
    HDU 5739 Fantasia
    HDU 5738 Eureka
    HDU 5734 Acperience
    HDU 5742 It's All In The Mind
    POJ Euro Efficiency 1252
    AtCoder Beginner Contest 067 C
    AtCoder Beginner Contest 067 D
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1715077.html
Copyright © 2011-2022 走看看