software design question

John Roth newsgroups at jhrothjr.com
Mon Feb 9 08:20:51 EST 2004


"Jorge Godoy" <godoy at ieee.org> wrote in message
news:l69kf1-9e8.ln1 at wintermute.g2ctech...
> On Sunday 08 February 2004 01:24 John Roth wrote in
> <102bb30d5422pd4 at news.supernews.com>:
>
> > I suspect you've got the callback logic backwards. The basic
> > layering pattern is "call down, notify up," and it's the upper
> > layer's responsibility to request the notification.
> >
> > To explicate: the GUI module calls the domain
> > module for service. It also calls the domain module
> > to register callbacks, passing the function that it
> > want's called.
> >
> > When the domain layer has something it wants to
> > tell the GUI layer, it simply runs a (possibly empty)
> > list of callback functions.
>
> Can you exemplify it?
>
> I'm very interested on such approach and I'm going to read a little
> more about it, but examples in Python would help a lot.

A simple notify class:
---------------------------------------------
class Notify(object):
    def __init__(self):
        self.listenerList = []

    def sendMessage(self, event):
        for callback in self.listenerList:
            callback(event)
        return

    def addListener(self, callback):
        self.listenerList.append(callback)

    def removeListener(self, callback):
        self.listenerList.remove(callback)
---------------------------------------------

Here's a place where I instantiate it:

--------------------------------------------------------------
class ALocation(object):
    def __init__(self):
        self._latitude = LatitudeContainer()
        self._longitude = LongitudeContainer()
        self._placeName = StringContainer()
        self._titleNotifier = A4Base.Notify.Notify()
        self._latitude.addListener(self._titleUpdated)
        self._longitude.addListener(self._titleUpdated)
---------------------------------------------------------------

and the routine that uses this notifier:

--------------------------------------------------------
    def _titleUpdated(self, event):
        self._titleNotifier.sendMessage(event)
--------------------------------------------------------

LatitudeContainer and LongitudeContainer both derive
from BaseContainer (through StringContainer, but that
doesn't add anything to this example):

-------------------------------------------------------
class BaseContainer(object):
    def __init__(self):
        self._notifier = A4Base.Notify.Notify()
    def addListener(self, callback):
        return self._notifier.addListener(callback)
    def removeListener(self, callback):
        return self._notifier.removeCallback(callback)
    def _sendMessage(self, message):
        return self._notifier.sendMessage(message)
---------------------------------------------------------

here's the routine in LatitudeContainer that fires
off the notification:

------------------------------------------------------
    def _setStringValue(self, value):
        self.parseLat(value)
        if self._stringValue != value:
            self._stringValue = value
            self._sendMessage(value)
-------------------------------------------------------

HTH

John Roth



>
>
> TIA,
> -- 
> Godoy.     <godoy at ieee.org>





More information about the Python-list mailing list