title: Delphi Singleton Pattern
body: <p>I know this is discussed many times everywhere i the community but I just can't find a nice and simple implementation of a Singleton Pattern in Delphi.
  I have an example in C#:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public sealed class Singleton {
    // Private Constructor
    Singleton() { }
  
    // Private object instantiated with private constructor
    static readonly Singleton instance = new Singleton();
  
    // Public static property to get the object
    public static Singleton UniqueInstance {
      get { return instance; }
    }
  }
  </code></pre>
  
  <p>I know there is no solution as elegant as this in Delphi and I saw a lot of discussion about no being able to correctly hide the constructor in Delphi (make it private) so we would need to override the NewInstance and FreeInstance methods. Something along those lines I believe is the implementation I found on <a href="https://ibeblog.com/2010/08/18/delphi-singleton-patterns/" rel="nofollow noreferrer">ibeblog.com - "Delphi: Singleton Patterns"</a>:</p>
  
  <pre class="lang-pascal prettyprint-override"><code>type
    TTestClass = class
    private
      class var FInstance: TTestClass;
    public                              
      class function GetInstance: TTestClass;
      class destructor DestroyClass;
    end;
  
  { TTestClass }
  class destructor TTestClass.DestroyClass;
  begin
    if Assigned(FInstance) then
      FInstance.Free;
  end;
  
  class function TTestClass.GetInstance: TTestClass;
  begin
    if not Assigned(FInstance) then
      FInstance := TTestClass.Create;
    Result := FInstance;
  end;
  </code></pre>
  
  <p>What would be your suggestion regarding the Singleton Pattern? Can it be simple and elegant and thread safe?</p>
  
  <p>Thank you.</p>