Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
3
Erster Beitrag:
vor 3 Jahren, 8 Monaten
Letzter Beitrag:
vor 3 Jahren, 7 Monaten
Beteiligte Autoren:
Danny Lauwers, Alexandre Leclerc

[WD19/WDM19] Observer / Observable LST 98 ??

Startbeitrag von Danny Lauwers am 21.10.2014 11:53

Hello,

In the index of the LST 98 I found this interesting piece of text.
http://www.pcsoft.fr/lst/index.html

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

Antworten:

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

von Alexandre Leclerc - am 21.10.2014 15:32
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

von Danny Lauwers - am 22.10.2014 12:05
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.