1

I know the single responsibility principle states that one class should only have one responsibility or one reason to change. Does this mean a class with many aggregated member variables is violating this principle? By this I mean when a class delegates some of its operations to it's aggregated member variables is that violating the SRP? Or are these aggregated members considered responsible for only their operations and the class that contains them has nothing to do with those operations?

6
  • 2
    A concrete example of what you're asking about would be useful.
    – nhgrif
    Apr 10, 2016 at 0:52
  • Say you have a playerCharacter class for a video game. In this class you have aggregated members of characterAi, characterAnimation, and characterController. I just wanted to confirm that even though the playerCharacter class has all these aggregated members that this alone does not violate the srp.
    – Jason
    Apr 10, 2016 at 1:08
  • That's true. But if playerCharacter constructs characterAi, characterAnimation, and characterController as well as uses them you are asking a bit much of playerCharacter. Apr 10, 2016 at 1:15
  • Okay. Ya I was curious because in my mind you need some interaction between classes in order to keep you main program as readable as possible. If you had tons of separate small classes I feel like that would decrease readability of main when you have to bring all these classes in and connect them.
    – Jason
    Apr 10, 2016 at 1:20
  • You can have tons of small separate classes and still have a fairly clean main. One pattern is to construct all objects that should have one instance in main. Pass them to each other and build an object graph. If this becomes long start abstracting the construction. Then call one method on one object to start the whole thing working. Apr 10, 2016 at 1:27

1 Answer 1

0

How much the class has in it is not the point. It's what the class does. What it's for. What it's responsibility is. The class might not expose any of those aggregated members. It might only have one method. And all those members are needed to do that one job. As long as it has one job it's not violating the single responsibility principle.

That said, it is possible to overly flatten things under one job that should be grouped and abstracted away under other responsibilities. If changes in those abstracted responsibilities impact our class they were not properly abstracted.

Having a reference to an object should only expose you to it's interfaces. Not to it's internal changes.

1
  • Okay that clears things up a little. Thank you very much.
    – Jason
    Apr 10, 2016 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.