Singleton pattern

This is an old revision of Singleton pattern, edited by Thaddy at . The current revision is 150505.

/* simple singleton */

{
  Implements singleton pattern by using FreePascal's property feature.
  
  A global block can declare properties, just as they could be defined 
  in a class. The difference is that the global property does not need 
  a class instance: there is only one instance of this property. 
}

{$mode objfpc}{$H+}
uses 
  classes;
  
  // this can be moved to a separate unit
  function InvokeList:TStringlist;inline;
  {$push}{$J+}const value:TStringlist = nil;{$pop}
  begin
    if value = nil then value := TStringlist.create;
    Result := Value;
  end;

  property List:TStringlist read InvokeList;

begin
  List.Add('text added');
  Writeln(List.Text);
  readln;
  List.free;
end.