Single responsibility principle

A microservice should always have a small set of responsibilities. This definition is based on a principle called the single responsibility principle, which states that a class must have only one reason to change. In other words, a class with more than one motive to change would have more responsibilities, and would not be cohesive. This would create problems, such as high coupling between the responsibilities of a class, difficulty in reusing code, and difficult maintenance. 

A class that had methods that are responsible for calculation, as well as methods for formatting the result, would be an example of a class with little cohesion. Let's look at the following example:

class Insurance {    
public double calculateCotation (...) {...}
public String generatePDF (...){...}
public String generateXLS (...){...}
}

The first method (calculateCotation) is directly related to the business domain and the others (generatePDF and generateXLS) are related to the presentation of the calculated values.

The SRP principle is part of a set of five principles called SOLID principles, which were popularized by Robert Cecil Martin (colloquially known as Uncle Bob) after the publication of his book Agile Software Development: Principles, Patterns, and Practices. In fact, these five principles are a subset of several principles reported in the book. The SOLID principles are principles of object-oriented programming, a paradigm whose purpose is to keep code more organized and easy to read. We can make an association between the word "solid" and the expression "solid code". Each of the five letters of the acronym SOLID is an initial of a principle:

 Single responsibility principle (SRP)
 Open closed principle (OCP)
 Liskov substitution principle (LSP)
 Interface segregation principle (ISP)
 Dependency inversion principle (DIP)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.44.229