其他分享
首页 > 其他分享> > 【Delphi】UEventBus

【Delphi】UEventBus

作者:互联网

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.


标签:begin,end,IEventBus,Delphi,subscriber,UEventBus,subscribers,procedure
来源: https://blog.csdn.net/zte1055889498/article/details/110531715