oo-70-245-1

oo-70-245-1 Git Source Tree


Root/Design Pattern Examples/Singleton.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace singleton
{
    class Log
    {
        private static Log instance;
        private static object syncRoot = new Object();
 
        public static Log GetInstance()
        {
            lock (syncRoot)
            {
                if (instance == null)
                    instance = new Log();
            }
            return instance;
        }
 
        public void WriteLog(string msg)
        {
            if (instance == null)
                throw new Exception("Exception: Log not initilized!");
            Console.WriteLine("Writting out to log....");
        }
 
    }
     
    class Program
    {
        static void Main(string[] args)
        {
            Log.GetInstance().WriteLog("test");
        }
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.11986s using 11 queries.