PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2024 → [WD19/WDM19] Observer / Observable LST 98 ??
[WD19/WDM19] Observer / Observable LST 98 ??
Débuté par Danny Lauwers, 21 oct. 2014 13:53 - 2 réponses
Posté le 21 octobre 2014 - 13:53
Hello,

In the index of the LST 98 I found this interesting piece of text.
<a class="ExternalLink" rel="nofollow" target="_blank" href="http://www.pcsoft.fr/lst/index.html">http://www.pcsoft.fr/lst/index.html</a>

OBJECT PROGRAMMING: BUILDING DESIGN PATTERN OBSERVER
(WINDEV, WINDEV MOBILE)

The pattern "Observer / Observable" allows to set up a subscription relationship between one object ("observable") and objects ("observers"). Observers subscribe to the observable. When a value of the observable changes, all observers are notified of the change without having to ask periodically.

I have no subscription to the LST because it is in French and my French is very bad :-(
Long live Google Translate !

Does anybody know what technique or technology they are using for this ?

Thanks
Danny
Posté le 21 octobre 2014 - 17:32
Hi Danny,

I don't know yet how PCSoft implemented it (because it will be received at the end of the month), but this is very trivial. We use that pattern since couple years.

Here is an simple example of a synchronous observer/subscriber pattern that I translated from French (I deleted a bunch of comments and only left some that I translated in English):

STSubscriber is Structure nID is int // Unique ID of the subscriber sSubject is string // Subject of the subscription proc is Procedure // Procedure to call END CPObservator is Class GLOBAL PUBLIC CONSTANT mg_taSubscribers is associative array (WithDuplicates) of STSubscriber // List of subscribers to the different topics END PROCEDURE Constructor() PROCEDURE Destructor() PROCEDURE GLOBAL Subscribe(sSubject is string, proc is Procédure) stASubscriber is STSubscriber stASubscriber.nID = GetIdentifier() stASubscriber.sSubject = Upper(sSubject) stASubscriber.proc = proc WHEN EXCEPTION IN mg_taSubscribers[stASubscriber.sSubject] = stASubscriber DO Trace(ExceptionInfo()) END RESULT stASubscriber.nID PROCEDURE GLOBAL Unsubscribe(nID is int) FOR EACH stASubscriber OF mg_taSubscribers IF stASubscriber.nID = nID THEN Delete(mg_taSubscribers,CurrentElement) RESULT True END END RESULT False // Publish a message for a subject. The observers will be notified immediately (not Async.) PROCEDURE GLOBAL Publish(LOCAL sSubject is string, Msg) sSubject = Upper(sSubject) FOR EACH stASubscriber OF mg_taSubscribers = sSubject stASubscriber.proc(sSubject, Msg) END
It is very simple and has he advantage to publish any kind of message or even objects. The only dependence of all the classes is the observator class.

Example of use:
// Somewhere in the code: CPObservator.Subscribe("MyMessage.Particle1",_OnParticleChange) // Somewhere else in the code: CPObservator.Publish("MyMessage.Particle1",NewValeur)
Based on this principle you can expend a lot for the features you might need (we did not need more that that right now):
- A async mode called in a thread (but the nature of messages will be more limited).
- An history enabled mode that keep all previous messages send so if a new subscriber comes in he can receive all the previous messages. This is quite strait forward to make.

I'm curious to see how PCSoft will have done the job. Answer in couple weeks.

Best regards,
Alexandre Leclerc
Posté le 22 octobre 2014 - 14:05
Alexandre,

Thanks for the input, as I read the code... I go like :rolleyes:, Of course why did't I thing about that. It is indeed not that difficult. I was thinking, maybe PC-Soft has special commands for this to hook up events of variables to procedures or so ?

Thanks again for showing this here, it will come in handy once in a while !!

Have a great day
Danny