The SOLID principle is supposed to be the underlying framework for object oriented programming. The "S" part stands for: "Single responsibility principle" which in wikipedia is defined as:
A class should only have a single responsibility, that is, only changes to one part of the software's specification should be able to affect the specification of the class
I understand this conceptually, however in this repo an example is given of the single responsibility principle where a class, since is should only have one job, should be limited to one method (besides the __init__
). The developer of this small repo seems to advocate a one-method per class principle, which would ensure single responsibility. A new method should be part of a new class, and then the facade pattern can be used to basically instantiate the lower-level class within the higher level class.
In practice however, there are tons of examples in python
where classes have many responsibilities. Take, for instance a simple composite pattern:
class ChildElement:
def __init__(self, name):
self.name = args[0]
def printDetails(self):
print("\t", end = "")
print(self.name)
class CompositeElement:
def __init__(self, name):
self.name = name
self.children = []
def appendChild(self, child):
self.children.append(child)
def removeChild(self, child):
self.children.remove(child)
def printDetails(self):
print(self.name)
for child in self.children:
print("\t", end = "")
child.printDetails()
The class CompositeElement
seems to contain several methods, one to append a child, one to remove a child and one to print the details of the composite. This seems to violate the single responsibility principle as outlined by the example in the repo above.
What does single responsibility truly imply in the context of an object oriented python program? Is it really having one method per class? If so why do so many GoF design patterns not abide by this rule?
Thanks