Einzelnen Beitrag anzeigen

Benutzerbild von Stevie
Stevie

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

AW: Lazy Initialization im Type?

  Alt 19. Feb 2015, 11: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