This unit implements the observer pattern.
Note: This is the managed version. There is also a manual version.
TManagedEventHandler enables multiple objects to subscribe to the same event.
When an observer subscribes to a TManagedEventHandler event, it receives an interface that represents the subscription.
- When either the observer or the event handler releases the subscription object, the subscription ends.
- When the subscription ends, the event ISubscription.OnEnd() fires.
- You can see the state of the subscription by calling ISubscription.IsActive().
Use TNotifyEvents or implement one of the generics or use them as examples for writing your own explicit event handlers.
To add an event handler to a subject (the object that observers are observing) do the following… (In this example a TNotifyEvents, which does not take parameters.)
In the subject add:
TMySubject = class private fOnChanged: TNotifyEvents; public property OnChanged: TNotifyEvents read fOnChanged;
In the constructor add…
fOnChanged := TNotifyEvents.Create(Self);
…and don’t forget to free it in the destructor.
An observer has the following:
MySubj: TMySubject; fSubscription: ISubscription; procedure CallbackMethod(Sender: TObject);
fSubscription := MySubj.OnChanged.Subscribe(CallbackMethod);
fSubscription := nil;
To fire the event, the TMySubject object calls: