Observer Pattern 3-23
Published Date: 2020-12-01 21:32:12Z
In programming, there is a lot of case that need to update information if data had changed. Well, it is Observer Pattern that I will descript in this post.
Feature:
- update information if data has changed
In a scene, students need to received teachers' information in time (student > teacher). I will direct to show about student's and teacher's base class.
Base class
class Student(object):
def __init__(self, name: str):
self.name = name
def update(self):
print("Student have received the info.")
class Teacher(object):
def __init__(self):
self.info = ""
def send(self, s: str):
self.info = s
print("Teacher has send the info. The info is %s." % (s, ))
Well, base class had built. Next step, I suppose that Teacher must have a list of student.
Teacher class
class Teacher(object):
self.info: str = ""
self.students: List[Student] = []
def addStudent(self, s: Student):
self.students.append(s)
def removeStudent(self, s: Student):
self.students.remove(s)
def send(self, s: str):
.....
It is important that Teacher's send function have to send the message to students. ```py class Teacher(object): ...... def send(self, s: str): self.info = s print("Teacher has send the info. The info is %s." % (s, )) for s in self.students: s.update() ``` All students have know teacher's info in time. But "info" does not transmit to student. Students only have a message and do not know details about the info.
class Student(object):
......
def update(self,t: Teacher):
print("Student have received the info.The info is %s." % (t.info, ))
class Teacher(object):
......
def send(self, s: str):
self.info = s
print("Teacher has send the info. The info is %s." % (s, ))
for s in self.students:
s.update(self)
It look like have solve the problems. Merge the code.
Merge Code
class Student(object):
pass
class Teacher(object):
pass
class Student(object):
def __init__(self, name: str):
self.name = name
def update(self,t: Teacher):
print("%s have received the info.The info is %s." % (self.name, t.info, ))
class Teacher(object):
def __init__(self, name: str):
self.info: str = ""
self.students: List[Student] = []
def addStudent(self, s: Student):
self.students.append(s)
def removeStudent(self, s: Student):
self.students.remove(s)
def send(self, s: str):
self.info = s
print("Teacher has send the info. The info is %s.\n" % (s, ))
for s in self.students:
s.update(self)
if __name__ == "__main__":
s1 = Student("John")
s2 = Student("Amy")
s3 = Student("Mike")
t1 = Teacher("White")
t1.addStudent(s1)
t1.addStudent(s2)
t1.addStudent(s3)
t1.send("Today is Sunday")
print()
t1.removeStudent(s1)
t1.send("John is out of class")
print()
The code output:
Output
Teacher has send the info. The info is Today is Sunday.
John have received the info.The info is Today is Sunday.
Amy have received the info.The info is Today is Sunday.
Mike have received the info.The info is Today is Sunday.
Teacher has send the info. The info is John is out of class.
Amy have received the info.The info is John is out of class.
Mike have received the info.The info is John is out of class.
It is end of Observer Pattern.
Extra
class Student(object):
pass
class Teacher(object):
pass
class Student(object):
def __init__(self, name: str):
self.name = name
def update(self,t: Teacher):
print("%s have received the info.The info is %s." % (self.name, t.info, ))
class Teacher(object):
def __init__(self, name: str):
self.info: str = ""
self.students: List[Student] = []
def addStudent(self, s: Student):
self.students.append(s)
def removeStudent(self, s: Student):
self.students.remove(s)
def notifyAll(self):
for s in self.students:
s.update(self)
def send(self, s: str):
self.info = s
print("Teacher has send the info. The info is %s.\n" % (s, ))
self.notifyAll()
if __name__ == "__main__":
s1 = Student("John")
s2 = Student("Amy")
s3 = Student("Mike")
t1 = Teacher("White")
t1.addStudent(s1)
t1.addStudent(s2)
t1.addStudent(s3)
t1.send("Today is Sunday")
print()
t1.removeStudent(s1)
t1.send("John is out of class")
print()