The Delphi compiler in Berlin 10.1 introduces the concept of weak references.
With reference to the code below, without the [weak] attribute, when one and two is nil'd, the destructor is not called. With the [weak] attribute, the destructor is called. So, the weak references in the Delphi wiki now extends to Win32/Win64 (possibly iOS/OSX too?)
program Project1; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; type IMyInterface = interface procedure AddObjectRef(AMyInterface: IMyInterface); end; TObjectOne = class (TInterfacedObject, IMyInterface) private FName: string; [weak]anotherObj: IMyInterface; public procedure AddObjectRef(AMyInterface: IMyInterface); constructor Create(const AName: string); destructor Destroy; override; end; procedure main; var one, two: IMyInterface; begin one := TObjectOne.Create('one'); two := TObjectOne.Create('two'); one.AddObjectRef (two); two.AddObjectRef (one); two := nil; one := nil; end; { TObjectOne } procedure TObjectOne.AddObjectRef(AMyInterface: IMyInterface); begin anotherObj := AMyInterface; end; constructor TObjectOne.Create(const AName: string); begin inherited Create; FName := AName; end; destructor TObjectOne.Destroy; begin WriteLn(FName, ' is being destroyed'); inherited; end; begin main; end.
In 2017, with the release of Delphi 10.2 Tokyo, Embarcadero introduced a specialized implementation of the Observer pattern into the System.Classes unit. While it has been in the wild for 9 years, it remains a "hidden" architecture for many, primarily because it serves as the invisible engine behind LiveBindings. Other than live bindings, you can also use the Observer pattern as a way to update component settings to the Windows registry, an .ini file, or persist it elsewhere.
System.Classes