Demo entry 6776235

Observer pattern

   

Submitted by anonymous on Nov 30, 2018 at 12:56
Language: Python. Code size: 604 Bytes.

import abc


class Observer(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def update(self, obj, *args, **kwargs):
        pass


class Observable:
    def __init__(self):
        self._observers = []

    def add_observer(self, observer):
        self._observers.append(observer)

    def remove_observer(self, observer):
        self._observers.remove(observer)

    def notify_observer(self, *args, **kwargs):
        for observer in self._observers:
            observer.update(self, *args, **kwargs)


class MyObserver(Observer):
    pass


observer = MyObserver()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).