unit UEventBus;
interface
uses
System.Classes;
type
IEventBus = interface(IUnknown)
procedure Notify(information: TObject);
end;
////////////////////////////////////////////////////////////////////////////////
TEventBus = class(TObject)
private
subscribers: TInterfaceList;
public
constructor Create;
destructor Destroy; override;
procedure Attach(subscriber: IEventBus);
procedure Detach(subscriber: IEventBus);
procedure Push(information: TObject);
end;
implementation
constructor TEventBus.Create;
begin
subscribers := TInterfaceList.Create;
end;
destructor TEventBus.Destroy;
begin
subscribers.Destroy;
inherited;
end;
procedure TEventBus.Attach(subscriber: IEventBus);
begin
if subscribers.IndexOf(subscriber) < 0 then begin
subscribers.Add(subscriber);
end;
end;
procedure TEventBus.Detach(subscriber: IEventBus);
begin
if subscribers.IndexOf(subscriber) > -1 then begin
subscribers.Remove(subscriber);
end;
end;
procedure TEventBus.Push(information: TObject);
var
i: Integer;
begin
with subscribers do begin
for i := 0 to Count - 1 do begin
IEventBus(Items[i]).Notify(information);
end;
end;
end;
end.
内容来源于网络如有侵权请私信删除
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!