Observer
Observer is an abstract base class used for implementing the
Observer design pattern in Python. subclasses of the Observer class
should implement the update method, which is called whenever a
subject the observer is watching changes.
Overview
The Observer class provides a template for creating objects that
watch or monitor other objects (subjects). If a subject changes, it
calls the update method in its observers.
Example
The following is an example demonstrating how to implement an instance
of Observer class by implementing the update method.
class CustomObserver(Observer):
def update(self, subject: Any):
print(f"Subject {subject} has changed.")
Limitations
The Observer class is an abstract base class, so it cannot be
instantiated on its own. Instead, you need to create a subclass and
implement the update method.
Follow-up Questions:
What are the specification and role of the subject parameter in the
updatemethod?What exact changes in the subject cause the
updatemethod to be called?