In this article, we will discuss about Single Responsibility Principle and how to implement it in Java
What is Single Responsibility Principle(SRP)?
The single responsibility principle states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class.
All its services should be narrowly aligned with that responsibility
SRP is the first principle from SOLID principles.
As per SRP, there should not be more than one reason for a class to change, or a class should always handle single functionality.
If you put more than one functionality in one Class in Java, it introduces coupling between the functionalities and even if you are trying to change one functionality, there is chance of you breaking the coupled functionality, which requires another round of testing to avoid any surprise in production environment.
Let’s look at below example to understand it better.
public class TicketReservation { public void createTicketReservation(ReservationDetails reservation){ // Create a reservation new OnlineReservationDAO.createReservation(reservation); } }
This class has clearly just one responsibility and it doesn’t violate the SRP as its only responsibility is to call a data access object to create a ticket reservation. Since it only has one responsibility, it also has only one reason to change.
If we added code to the same class so that the user receives an email once the reservation is saved, it would look like this:
public class TicketReservation { public void createTicketReservation(ReservationDetails reservation){ // Create a reservation new OnlineReservationDAO.createReservation(reservation); } public void sendEmailToCustomer(){ // Code to send email } }
This looks simple, right?
Yes, but we just introduced a new reason for this class to change.
Every time we needed to change how the class does the Ticket reservation or how it sends email, we’ll need to change it.
We don’t want a class to be impacted by these two completely different reasons. The new code above violates the SRP.
So let’s see how to avoid the SRP violation:
One way to satisfy the SRP in the example above will be to create a new class with the code that takes care of emailing reservation details.
Code that generates emails should not be part of this TicketReservation class.
Advantages of Single Responsibility Principle
The primary value of software is ease of change. The secondary is functionality, in the sense of satisfying as much requirements as possible, meeting the user’s needs.
However, in order to achieve a high secondary value, a primary value is mandatory. To keep our primary value high, we must have a design that is easy to change, to extend, to accommodate new functionalities.
Following SRP helps in making sure we can accomodate change and build better software.
Thanks for visiting !!
Reference:
https://en.wikipedia.org/wiki/Single_responsibility_principle
https://lostechies.com (Image Reference)
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post
#
#