Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Responsibility Principle : class level or method level

I have problem in understanding Single Responsibility Principle . Should SRP be applied at class level or at method level. Lets say i have Student Class ,i need to create student , update student and delete student. If I create a service class that has methods for these three actions does this break SRP principle.

like image 783
sumedha Avatar asked Nov 28 '15 07:11

sumedha


People also ask

What is a single responsibility class?

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.

Why should a class focus on a single responsibility?

The reason it is important to keep a class focused on a single concern is that it makes the class more robust. Continuing with the foregoing example, if there is a change to the report compilation process, there is a greater danger that the printing code will break if it is part of the same class.

What is the principle behind the Single Responsibility Principle?

The Single Responsibility Principle (SRP) The idea behind the SRP is that every class, module, or function in a program should have one responsibility/purpose in a program. As a commonly used definition, "every class should have only one reason to change". The class above violates the single responsibility principle.

What is Single Responsibility Principle example?

Single-responsibility Principle (SRP) states: A class should have one and only one reason to change, meaning that a class should have only one job. For example, consider an application that takes a collection of shapes—circles, and squares—and calculates the sum of the area of all the shapes in the collection.


1 Answers

SRP is at both at class and method level.So if you ar talking about student class then only responsibility it has in this case to do CRUD on student entity.At the same time when you talk about methods the you should not have an InsertStudent method and do both Update and Insert in it based on ID .That breaks SRP.But if you have InsertStudent which inserts and UpdateStudent which updates it follows SRP

like image 142
Jags Avatar answered Oct 20 '22 11:10

Jags