Einzelnen Beitrag anzeigen

Benutzerbild von Stevie
Stevie

Registriert seit: 12. Aug 2003
Ort: Soest
4.016 Beiträge
 
Delphi 10.1 Berlin Enterprise
 
#3

AW: Lazy Initialization im Type?

  Alt 19. Feb 2015, 12:56
Delphi-Quellcode:
type
  UnknownObject = record
  private
    fInstance: IUnknownObject;
    procedure EnsureInstance;
  public
    class operator Implicit(const value: UnknownObject): IUnknownObject;
    procedure UnknownObject;
  end;

procedure UnknownObject.EnsureInstance;
begin
  if not Assigned(fInstance) then
    fInstance := TUnknownObject.Create;
end;

class operator UnknownObject.Implicit(
  const value: UnknownObject): IUnknownObject;
begin
  value.EnsureInstance;
  Result := value.fInstance;
end;

procedure UnknownObject.UnknownObject;
begin
  EnsureInstance;
  fInstance.UnknownObject
end;
Stefan
“Simplicity, carried to the extreme, becomes elegance.” Jon Franklin

Delphi Sorcery - DSharp - Spring4D - TestInsight
  Mit Zitat antworten Zitat