# Copyright (c) 2000-2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
""" generic design patterns """

__revision__ = '$Id: dp.py,v 1.2 2001/12/07 16:01:33 syt Exp $'


## Observer/Observable ########################################################

class Observable :

    def __init__(self) :
        self.__observers = []
        self.changes = []
        self.__id = str(self.__class__).split('.')[-1]
        
    def attach_observer(self,observer):
        if observer not in self.__observers :
            self.__observers.append(observer)

    def detach_observer(self,observer):
        i = 0
        for o in self.__observers:
            if o.__dict__ is observer.__dict__:
                self.__observers.pop(i)
                break
            i += 1
            
    def notify_observers(self) :
        """
        if the observable class name is A (or module.A), this method will call
        A_update method on all observers
        """
        func = 'o.%s_update(self,self.changes)'%self.__id
        for o in self.__observers :
            try:
                eval(func)
            except AttributeError, e:
                import traceback
                traceback.print_exc()
                print "%s doesn't support %s_update"%(o.__class__, self.__id)
        self.changes = []


class ObserverInterface :
    """
    if the observer observes object whose class name is A (or module.A),
    A_update method will be called on observable update
    """

##  ###########################################################################
