tags:

views:

86

answers:

1

I'm trying to understand what a responsibility actually is so I want to use an example of something I'm currently working on. I have a app that imports product information from one system to another system. The user of the apps gets to choose various settings for which product fields in one system that want to use in the other system.

So I have a class, say ProductImporter and it's responsibility is to import products. This class is large, probably too large.

The methods in this class are complex and would be for example, getDescription. This method doesn't simply grab a description from the other system but sets a product description based on various settings set by the user. If I were to add a setting and a new way to get a description, this class could change.

So, is that two responsibilities? Is there one that imports products and one that gets a description. It would seem this way, almost every method I have would be in it's own class and that seems like overkill.

I really need a good description of this principle because it's hard for me to completely understand. I don't want needless complexity.

+2  A: 

The 'responsibility' is defined in this principle as a reason to change. In this case the single responsibility of your class would be to import products. If the way of importing product changes, then the class should change.

The intention is to avoid having different things changing the same class at the same time. For example if your product importer class also defined its output format, it would then have two responsibilities, as likely the output format is totally unrelated to the mechanism of importing the data.

Now, that the class is huge and that getDescription() also sets a description are not a direct violation of the SRP, but of different principles. Namely, that you should avoid having huge classes (shows a lack of design) and each method should do a single thing (which would be kind of a more concrete version of the SRP.)

Vinko Vrsalovic
It "Shows a lack of Design" - This is the problem that I'm not sure how to correct. If the description method belongs in the class and I have to account for each variation depending on the various settings, how else can you design it. I don't see any other way.
You can have a Description class, whose responsibility would be to handle the variation. And so on, when a single responsibility is too big, split it in smaller responsibilities.
Vinko Vrsalovic