tags:

views:

92

answers:

4

I wish to follow the Single Responsibility principle in C++. However, as I break up classes, it seems that in order for classes to "see" each other, I have the following choices:

  1. Add many more accessors for each class
  2. Make classes friends of each other
  3. Improve the design (maybe the fact that I would have to do 1 or 2 indicates a flaw in my design)

The friend vs. accessors issue has probably been discussed before, but I did not know if one was more advantageous with regard to implementing Single Responsibility.

+1  A: 

You also have option 4: add more classes to represent the different roles/interactions between the classes.

That at least falls more in line with the Law of Demeter.

MSN
+4  A: 

I've always thought that rule was BS. Most classes have several responsibilities, and no harm done. Consider a bank account class - it might have the responsibilities:

  • maintain client details
  • allow for debit & credit transactions
  • provide current balance
  • report dubious transactions to security

Of course, these responsibilities will probably be implemented using other classes that the account is composed of.

anon
Depends what you define as responsibility. It is obviously easy to take it too far (as you demonstrate). I would say in this case it is the responsibility of the class to manage an account. You have to use your experience to decide the granularity of what you define as responsibility.
Martin York
@Martin I think of responsibilities as things that might appear on a CRC card - "manage the account" is too vague to be very useful.
anon
I wouldn't say the rule was BS. In your example, it just says that you should do exactly what you suggest in the last paragraph: delegate the various responsibilities to other classes, leaving the top-level class with the single responsibility of managing them.
Mike Seymour
@Neil Butterworth: Yes 'manage the account' is vague (but I don;t see a spec here). I would start with 'Manage the Account' as the area of responsibility. Then you would need to define what functionality that entails. But since responsibility is a vague term them I fine with a vague definition. At the point of implementation you better have it nailed down though.
Martin York
+3  A: 

If you must expose private data from one class to another, than make the second class a friend. Creating an accessor for your private data defeats the purpose of making it private in the first place. The single responsibility principal has no bearing on this.

Peter Ruderman
+1  A: 

Now that you have a group of classes that all need to work together, you should consider how they should work together. If it's via accessor functions or friends, then you're tightly coupling the classes. It would be difficult in the future to drop in a new class that does something different. It's also difficult to test the classes since they're all inter-dependent.

Consider creating an interface class(es) that defines how your classes should communicate. Unless there's some special privileges involved, this interface will also define how anyone else would communicate with them. This way, you break the classes' inter-dependency. Any future changes are localized to the class involved. Nobody else has to change (or maybe even recompile).

Kristo