Click here to Skip to main content
Click here to Skip to main content

Single responsibility principle (SRP)

By , 23 Jul 2012
 

SOLID principles:

Every class should have only one responsibility. 

Consider an example:

(not for production, illustrative purposes only)

class Calculator {
  public void add(int x, int y) {
    System.out.println(x + y);
  }
}

First of all, there's a word "calculator". "Calculator" means "a thing aimed to calculate something". Don't be confused with what Casio does. These are not calculators, these a calculating machines. Since calculator should calculate, and what we have in the code above appears to be a sort of calculating machine, the first idea to appear in out head should be "OK, the author of this class has lied, he wanted to call it a calculating machine". So, we read it as:

(not for production, illustrative purposes only)

class CalculatingMachine {
  public static void add(int x, int y) {
    System.out.println(x + y);
  }
}

Now everything looks great. It's time to remember SRP: every class should have only one responsibility. What responsibilities does this class have?

  • It is responsible for the logic to add 2 numbers
  • It is responsible for displaying the result
  • It is responsible for choosing the output stream

There are three responsibilities, so SRP is violated. The responsibilities described here are basically "the facts" I've mentioned in the very beginning. The idea is to write three classes so that each of the was responsible for one fact. (not for production, illustrative purposes only)

class Calculator { // this class is only responsible for adding numbers
  public static int add(int x, int y) {
    return x + y;
  }
}

class PrintStreamDecider { // this class is only responsible for deciding which PrintStream is to be used
  public static PrintStream getPrintStream() {
    return System.out;
  }
}

class ResultPrinter { // this class is only responsible for printing int values
  public static void printResult(int value) {
    PrintStreamDecider.getPrintStream().print(value);
  }
}

We also need a fourth class to implement a calculating machine:

class CalculatingMachine { // this class is only responsible for processing the add-two-numbers request
  public static void processTheAddCommand(int x, int y) {
    int result = Calculator.add(x, y);
    ResultPrinter.printResult(result);
  }  
}
  • CalculatingMachine is not aware of what the "add" operation is, it is only aware of the intention.
  • CalculatingMachine is not aware of what is PrintStream, it is not aware about how to print the result at all. It is only aware of the intention that result should be printed.

I'd like to emphasize that I understand that this code is completely awful. There are reasons I decided to use this approach.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

loki2302
Software Developer (Senior)
Russian Federation Russian Federation
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 3memberMrTortoise2 Aug '12 - 3:39 
Is it finished?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 23 Jul 2012
Article Copyright 2012 by loki2302
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid