Closed utterances-bot closed 1 year ago
Is this code violating the Single Responsibility Principle?
class BadStudent(Student):
"""A class that represents a bad student who gossips when the teacher goes out."""
def react(self, teacher: Teacher) -> None:
if teacher.state == "going out":
print(f" - BAD_STUDENT REACT: 😃 I'm gossiping in the class")
else:
print(f" - BAD_STUDENT REACT: I don't care!")
It depends on how you define the responsibility of the BadStudent
class. If the only responsibility of the BadStudent
class is to represent a bad student who gossips when the teacher goes out, then this code does not violate the Single Responsibility Principle (SRP). However, if the BadStudent
class has other responsibilities, such as managing the behavior of all types of students, then this code would violate the SRP because it is mixing the responsibility of managing the behavior of all types of students with the behavior of a BadStudent.
In general, SRP suggests that a class should have only one reason to change. If a class has multiple responsibilities, then changes to one responsibility may affect the other responsibilities, making the code more difficult to maintain and understand.
BP5 - Observer - Learn Design Pattern From Simple Things
Some students in a class are attentive, but others are easily distracted. The teacher is indifferent to them; for her, they are all observers and she just performs her duty.
https://hoangyell.com/bp5-observer-learn-design-pattern-from-simple-things/