![]() |
Lazy Initialization im Type?
Ich bin in Codefragmente mehrfach auf folgendes Pattern gestoßen:
Code:
Und genutzt wird es so.
unit UnitUnknownLazyInitializationPattern;
interface uses UnknownInterfaces, MoreUnknownUnits; type TFoo = class private FMyUnknownObject: UnknownObject; // Type without the letter "T"? function GetMyUnknownObject: IUnknownObject; public property MyUnknowObject: IUnknownnObject read GetMyUnknownObject; end; implementation { TFoo } function TFoo.GetMyUnknownObject: IUnknownObject; begin Result := FMyUnknownObject; end; end.
Code:
Der Type von UnknowObject instanziiert Objekte bei Methoden Zugriffe selbst und kümmert sich auch um deren Freigabe(InterfacedObject, Smartpointer, ?).
var
Foo: TFoo; begin Foo := TFoo.create; try Foo.MyUnknownObject.UnknownProcedure; // crash here, because UnknowProject still is nil finally Foo.free; end; end; Wie müsste die Implementierung von Type „UnknownObject“ aussehen, um diesen Effekt zu bekommen? |
AW: Lazy Initialization im Type?
Was denn für Codefragmente?
Ich kann mir folgende Lösung vorstellen:
Delphi-Quellcode:
type
TFoo = class private FMyUnknownObject: IUnknownObject; ...
Delphi-Quellcode:
function TFoo.GetMyUnknownObject: IUnknownObject;
begin if not Assigned(FMyUnknownObject) then FMyUnknownObject := TMyUnknownObject.Create; Result := FMyUnknownObject; end; |
AW: Lazy Initialization im Type?
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; |
AW: Lazy Initialization im Type?
Zitat:
![]() Dort gibt es auch eine Sache die ich erst eben bemerkt habe, innerhalb des Objektes wird auch noch direkt auf die Feldvariabel zu gegriffen. FPropertyChanged.Invoke(..), was ich in dem unknown Beispiel nicht berücksichtig hatte. @Stevie danke, für die Implementierung, wieder was gelernt. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:45 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz