I've decided to implement a simple case of the Observer pattern. I've gone about this considering you can only register 1 single observer in the Observable, instead of allowing multiple ones.
I would really like feedback about what'd be the proper way to return the listener in the run
method while still yielding values, if possible i'd like to allow some sort of method chaining
Overall I would like some criticism about this whole design and of course, anything else is on the table will be appreciated.
Code:
import time
class Observer:
def __init__(self):
self.lines = []
self.execution_time = None
def on_process(self, value):
line = f"<< processed: {value} >>"
self.lines.append(line)
return line
def on_done(self, value):
self.execution_time = value
class Observable:
def __init__(self):
self.listener = None
def register_listener(self, listener):
self.listener = listener
return self
def run(self, iterable):
s = time.time()
for v in iterable:
yield self.listener.on_process(v)
self.listener.on_done(time.time() - s)
if __name__ == '__main__':
obj = Observable().register_listener(Observer())
for x in obj.run(range(10)):
print(x)
listener = obj.listener
print("Number of lines:", len(listener.lines))
print("Execution time:", listener.execution_time)