Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you define a Single Responsibility?

I know about "class having a single reason to change". Now, what is that exactly? Are there some smells/signs that could tell that class does not have a single responsibility? Or could the real answer hide in YAGNI and only refactor to a single responsibility the first time your class changes?

like image 793
mmiika Avatar asked Oct 29 '08 07:10

mmiika


People also ask

How is single responsibility principle determined?

As the name suggests, this principle states that each class should have one responsibility, one single purpose. This means that a class will do only one job, which leads us to conclude it should have only one reason to change. We don't want objects that know too much and have unrelated behavior.

Could you provide an example of the Single Responsibility Principle?

A class should have only one reason to change. This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes. Each class will handle only one responsibility and if in the future we need to make one change we are going to make it in the class which handles it.

Why is single responsibility important?

The argument for the single responsibility principle is relatively simple: it makes your software easier to implement and prevents unexpected side-effects of future changes.

What is Single Responsibility Principle in agile?

Definition: An object should have a single responsibility. This means that a class should only have one reason to change. An example of how this might work is with a class that compiles and formats a report. This class has two responsibilities and two reasons the class might change.


2 Answers

The Single Responsibility Principle

There are many obvious cases, e.g. CoffeeAndSoupFactory. Coffee and soup in the same appliance can lead to quite distasteful results. In this example, the appliance might be broken into a HotWaterGenerator and some kind of Stirrer. Then a new CoffeeFactory and SoupFactory can be built from those components and any accidental mixing can be avoided.

Among the more subtle cases, the tension between data access objects (DAOs) and data transfer objects (DTOs) is very common. DAOs talk to the database, DTOs are serializable for transfer between processes and machines. Usually DAOs need a reference to your database framework, therefore they are unusable on your rich clients which neither have the database drivers installed nor have the necessary privileges to access the DB.

Code Smells

  • The methods in a class start to be grouped by areas of functionality ("these are the Coffee methods and these are the Soup methods").

  • Implementing many interfaces.

like image 89
David Schmitt Avatar answered Oct 09 '22 21:10

David Schmitt


Write a brief, but accurate description of what the class does.

If the description contains the word "and" then it needs to be split.

like image 22
Steven A. Lowe Avatar answered Oct 09 '22 19:10

Steven A. Lowe