type
THAL_ObserverItem =
class(TObject)
private
FEvent: TNotifyEvent;
public
constructor Create(iEvent: TNotifyEvent);
public
property Event: TNotifyEvent
read FEvent
write FEvent;
end;
THAL_Observer =
class(TComponent)
private
FListSubscriber: TObjectList;
protected
function IndexOfEvent(_Event: TNotifyEvent): Integer;
public
procedure NotifySubscriber(_Sender: TObject);
procedure RegisterSubscriber(_Event: TNotifyEvent);
procedure UnRegisterSubscriber(_Event: TNotifyEvent);
constructor Create(_Sender: TComponent);
override;
destructor Destroy;
override;
end;
[...]
{ THAL_ObserverItem }
constructor THAL_ObserverItem.Create(iEvent: TNotifyEvent);
begin
inherited Create;
FEvent := iEvent;
end;
{==============================================================================}
{$Region 'constructor THAL_Observer.Create(_Sender: TComponent);'}
constructor THAL_Observer.Create(_Sender: TComponent);
begin
try
inherited Create(_Sender);
FListSubscriber := TObjectList.Create;
except
on E:
Exception do begin
end;
end;
end;
{$EndRegion}
{==============================================================================}
{$Region 'destructor THAL_Observer.Destroy;'}
destructor THAL_Observer.Destroy;
begin
try
FreeAndNil(FListSubscriber);
inherited Destroy;
except
on E:
Exception do begin
end;
end;
end;
function THAL_Observer.IndexOfEvent(_Event: TNotifyEvent): Integer;
var i: Integer;
begin
for i := 0
to FListSubscriber.Count - 1
do
if @THAL_ObserverItem(FListSubscriber[i]).Event = @_Event
then
begin
Result := i;
Exit;
end;
Result := -1;
end;
{$EndRegion}
{==============================================================================}
{$Region 'procedure THAL_Observer.NotifySubscriber(_Sender: TObject);'}
procedure THAL_Observer.NotifySubscriber(_Sender: TObject);
var
i: integer;
begin
try
for i := 0
to FListSubscriber.Count - 1
do begin
THAL_ObserverItem(FListSubscriber[i]).Event(Self);
end;
except
on E:
Exception do begin
end;
end;
end;
{$EndRegion}
{==============================================================================}
{$Region 'procedure THAL_Observer.RegisterSubscriber(_Event: TNotifyEvent);'}
procedure THAL_Observer.RegisterSubscriber(_Event: TNotifyEvent);
begin
try
if IndexOfEvent(_Event) = -1
then
FListSubscriber.Add(THAL_ObserverItem.Create(_Event));
except
on E:
Exception do begin
end;
end;
end;
{$EndRegion}
{==============================================================================}
{$Region 'procedure THAL_Observer.UnRegisterSubscriber(_Event: TNotifyEvent);'}
procedure THAL_Observer.UnRegisterSubscriber(_Event: TNotifyEvent);
begin
try
if IndexOfEvent(_Event) > -1
then begin
FListSubscriber.Delete(IndexOfEvent(_Event));
end;
except
on E:
Exception do begin
end;
end;
end;
{$EndRegion}
{==============================================================================}