Singleton Pattern 2 : Singleton Pattern « Design Patterns « C# / CSharp Tutorial






using System;

  class Client
  {
    static void Main(string[] args)
    {
      try{
        Engine eng = Engine.GetEngine();
      }catch (Exception e){
        Console.WriteLine(e.Message);
      }
      
      try{
        Engine eng = Engine.GetEngine();
      }catch (Exception e){
        Console.WriteLine(e.Message);
      }
    }
  }
  sealed class Engine
  {
    private static bool instanceFlag = false;
    public static Engine GetEngine()
    {
      if (!instanceFlag)
      {  
        instanceFlag = true;
        return new Engine();
      }else{
        throw new Exception("An engine has already been created!");
      }
    }
    private Engine()
    {
      Console.WriteLine("An Engine");
    }
  }








28.1.Singleton Pattern
28.1.1.Singleton Pattern
28.1.2.Singleton Pattern 2