unit SingletonTest;
{$MODE ObjFpc}

interface 

uses
  Classes, SysUtils, EventLog;
  
type
  TSingleton = class(TObject)
    private
      FEventLog: TEventLog;
    public
	  constructor Create;
      class function GetInstance: TSingleton;
      destructor Destroy;override;
      property EventLog: TEventLog read FEventLog;
  end;
  
  TCamouflagedSingleton = class(TObject)
    private
      FEventLog: TEventLog;
    public
	  class function NewInstance: TObject; override;
	  destructor Destroy;override;
      property EventLog: TEventLog read FEventLog;
  end;
   
implementation
 
var
  Singleton: TSingleton;
  CamouflagedSingleton: TCamouflagedSingleton;
 
class function TSingleton.GetInstance: TSingleton;
begin
  if not Assigned(Singleton) then 
    Singleton := TSingleton.Create;
  Result := Singleton;
end;

constructor TSingleton.Create;
begin
  if Assigned(Singleton) then 
    raise Exception.Create('This constructor should not called from outside.');
  inherited Create;
  FEventlog := TEventlog.Create(nil);
  FEventLog.LogType := ltFile;
  FEventLog.Active := True;
  Singleton := Self;
end;

destructor TSingleton.Destroy;
begin
  FreeAndNil(FEventLog);
  inherited Destroy;
end;

class function TCamouflagedSingleton.NewInstance: TObject;
begin 
  if not Assigned(CamouflagedSingleton) then begin
    Result := inherited NewInstance;
	TCamouflagedSingleton(Result).FEventLog := TEventlog.Create(nil);
    TCamouflagedSingleton(Result).FEventLog.LogType := ltFile;
    TCamouflagedSingleton(Result).FEventLog.Active := True;
  end;
  CamouflagedSingleton := TCamouflagedSingleton(Result);
end;

destructor TCamouflagedSingleton.Destroy;
begin
  FreeAndNil(FEventLog);
  inherited Destroy;
end;
	  
end. 
