简体   繁体   中英

The single responsibility principle and aggregation

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM