SOLID Principles : Guidelines for software development :: SRP
The SOLID principles provide five guidelines that, when followed, can dramatically enhance the maintainability of software.
Single Responsibility Principle Intro |
The SRP states that each class or similar unit of code should have one responsibility only and, therefore, only one reason to change.
The Single Responsibility Principle (SRP) states that there should never be more than one reason for a class to change. This means that every class, or similar structure, in your code should have only one job to do.
- Everything in the class should be related to that single purpose.
- It does not mean that your classes should only contain one method or property.
- There may be many members as long as they relate to the single responsibility.
- It may be that when the one reason to change occurs, multiple members of the class may need modification.
- It may also be that multiple classes will require updates.
-
Application of the SRP can change your code considerably.
- One key change is that the classes in your projects become smaller and cleaner.
- The number of classes present in a solution may increase accordingly so it is important to organise them well using namespaces and project folders.
- The creation of classes that are tightly focused on a single purpose leads to code that is simpler to understand and maintain.
Benefits
- A further benefit of having small, cohesive classes is that the chances of a class containing bugs is lowered.
- This reduces the need for changes so the code is less fragile.
- As the classes perform only one duty, multiple classes will work together to achieve larger tasks.
- Along with the other principles this permits looser coupling.
- It can also make it easier to modify the overall software, either by extending existing classes or introducing new, interchangeable versions.
Download Examples |
Code Available Before SRP
NitrogenMeter.java
package com.javaskool.before;
public class NitrogenMeter
{
public double NitrogenSaturation;
public void ReadNitrogenLevel()
{
int raw = 2;
NitrogenSaturation = (double)raw / 5 * 10;
}
public boolean NitrogenLow()
{
return NitrogenSaturation <= 55;
}
public void ShowLowNitrogenAlert()
{
System.out.println("Nitrogen low ({0:F1}%)"+ NitrogenSaturation);
}
}
In above class, we are doing multiple job like reading nitrogen level, checking nitrogen level and showing alert. We should avoid doing this.
Code Available After SRP
NitrogenAlerter.java
package com.javaskool.after;
public class NitrogenAlerter {
public void ShowLowNitrogenAlert(NitrogenMeter meter)
{
System.out.println("Nitrogen low ({0:F1}%)"+ meter.NitrogenSaturation);
}
}
NitrogenSaturationChecker.java
package com.javaskool.after;
public class NitrogenSaturationChecker {
public boolean NitrogenLow(NitrogenMeter meter)
{
return meter.NitrogenSaturation <= 55;
}
}
NitrogenMeter.java
package com.javaskool.after;
public class NitrogenMeter
{
public double NitrogenSaturation;// { get; set; }
public void ReadNitrogenLevel()
{
int raw = 2;
NitrogenSaturation = (double)raw / 5 * 10;
}
}
Recent Comments