Questions tagged [single-responsibility-principle]

For questions about the Single Responsibility Principle in object-oriented programming, one of the SOLID principles coined by Robert C. Martin. It states that a module should have only one reason to change.

Robert Martin was inspired by David Parnas, Edsger Dijkstra (who coined the term Separation of Concerns) and Larry Constantine (who coined the terms Coupling and Cohesion). During the late 1990s, Martin consolidated their ideas into the Single Responsibility Principle.

Martin's definition of the SRP evolved to become,

Gather together the things that change for the same reasons. Separate those things that change for different reasons.

In keeping with the previous authors who inspired him, Martin notes that,

...this is just another way to define cohesion and coupling.

In contrast with the principle, the SRP is focused on people rather than functionality.

As you think about this principle, remember that the reasons for change are people. It is people who request changes. And you don’t want to confuse those people, or yourself, by mixing together the code that many different people care about for different reasons.

The SRP later became the first of Martin's .

445 questions
0
votes
4 answers

Unexpected output on a ANDING boolean expression in a class and a single if statement

I wrote a function for getting data out of a array. A part in that function is verification of the data, it checks if the key is NOT empty and if the array key does NOT exists (my bad after the edit). The function looks like this: public function…
Bas
  • 2,106
  • 5
  • 21
  • 59
0
votes
1 answer

Who is responsible for initialization of the single model components in MVC

I cant find a clear answer of my question. In MVC, all is pretty divided in model, view and controller. So usually i'll create a controller object which holds instances of the model and the view. Model and view don't know anything from each other…
0
votes
2 answers

Single Responsibility Principle and Service/Repository design

I am wondering how to best separate responsibilites in the following example. There is a Foo object that has some id and some reallySecretImportantData fields. Id is a simple identificator for referencing the object, while reallySecretImportantData…
ioreskovic
  • 5,531
  • 5
  • 39
  • 70
0
votes
1 answer

oop inheritance of method vs making object only to call method in class which invoke it

I have problem with judge witch approach is better from design, clean code == good practice. I load some data from files on start-up of my program and structure of classes looks like this: To be more specific IngredientFromXmlReader, PizzaReader,…
MyWay
  • 1,011
  • 2
  • 14
  • 35
0
votes
2 answers

Does this Enum violate the Single Responsibility Principle?

Firstly I assume that Java Enums should conform to the same practices as regular classes. I'm a little hazy on the meaning of "Single responsibility". With the following code, I'm wondering if the hasSucceed method is the violation as it's logic…
0
votes
2 answers

Which is the better way to pass additional data for a changed workflow?

Let entity A relates to entities B and C both as 1:1..* At the same time B contains only part of C's key, but together with A's key it is possible to relate B to C. Translation to human language: Accounting Center (A) consists of Branches (C), and a…
0
votes
1 answer

What's variant better? SOLID - SRP and interfaces

What variant is better? SOLID - SRP or the one with the interface? class Some { private final IValidator validator; public Some(IValidator validator) { this.validator = validator; } public void doSomething() { if…
0
votes
1 answer

Single Responsibility Principle Implementation

I have a SQLStatement class which builds up sql query and supports bind parameters (which is stored to the property $params). I have a PDOAdapter class which acts just like PDO but has additional features such as prepare_query(SQL Statement $sql).…
0
votes
1 answer

How should I break down this huge function into smaller parts

I am trying to understand good design patterns in Python and I cannot think of a way to break this huge function into smaller parts without making the code cluttered, overly complex or plain ugly. I didn't want to clutter my question by posting the…
0
votes
2 answers

Refactoring csv reading, parsing and object initializing code snippet

I try to refactor some old code which I use to read data from CSV file, parse every line and initialize an object instance for every line in the CSV file and save every object in a list. I also try learn S.O.L.I.D principles and apply them. As…
0
votes
1 answer

Is SRP related to possible actions of a single class or to their implementation in it?

I've read in a lot of places that SRP is a good principle to apply, and I wanted to apply it in my projects. Actually, it's a game, and my objects have basically two functions update and draw, so they have two responabilities (defined as a "reason…
0
votes
1 answer

Actions across partial views on a shared controller not firing

I have several pieces of reusable code that I am trying to separate. The problem I am having is that two are directly related but are on different parts of the page and I would like them to be in separate partial views, so I have two partial views…
Robert
  • 4,306
  • 11
  • 45
  • 95
0
votes
1 answer

Applying SRP causing problems. How to?

I am creating my own Query Builder using php which can also execute queries and get results from the database. At first I had this functionality within one class, however after thinking it through I believe the QueryBuilder and the DBProcessor…
0
votes
1 answer

Best way to populate an object with properties pulled from a database

I'm using PHP 5.4. I'm in the process of trying to make my application more SOLID. I'm currently going through my objects and making sure they follow SRP. I'm stuck on how to handle populating my object with properties, especially properties that…
Jeff
  • 1,152
  • 5
  • 14
  • 27
0
votes
1 answer

Is this class violating Single Responsibility

The functionality I wanted to implement is to retrieve some data from database and create a text file out of the data based on some parameters. I created a FileProcessor class which does this. public class FileGenerator { public…
Ben10
  • 1
  • 2