Single Responsibility Principle
Every software component should have one and one responsibility only
Software component can be class, method or module
Example, a swiss-army knife is a multipurpose tool that violates the single responsibility principle of software development, instead a knife is a good example that follows Single responsibility (as it can only be used for cutting unlike the Swiss-army knife that can be used for cutting, opening can, as a master key, scissors,etc)
Since the change is constant be it in the real world or in software development, the definition of the Single Responsibility Principle also changes accordingly
Every software component should have one and only one reason to change
There are three reasons for which change can occur in the below class Employee
- Change in Employee attribute
- Change in Database
- Change in Tax calculation
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; /** * Employee class has details of employee * This class is responsible for saving employee details, getting tax of * employee and getting * details of employee like name, id, address, contact, etc. */ public class Employee { private String employeeId; private String employeeName; private String employeeAddress; private String contactNumber; private String employeeType; public void save() { String insert = MyUtil.serializeIntoString(this); Connection connection = null; Statement statement = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc://mysql://localhost:8080/MyDb", "root", "password"); statement = connection.createStatement(); statement.execute("insert into employee values (" + insert + ")"); } catch (Exception e) { e.printStackTrace(); } } public void calculateTax() { if (this.getEmployeeType().equals("fulltime")) { // tax calculation for full time employee } else if (this.getEmployeeType().equals("contract")) { // tax calculation for contract type employee } } public String getEmployeeId() { return employeeId; } public void setEmployeeId(String employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getEmployeeAddress() { return employeeAddress; } public void setEmployeeAddress(String employeeAddress) { this.employeeAddress = employeeAddress; } public String getContactNumber() { return contactNumber; } public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber; } public String getEmployeeType() { return employeeType; } public void setEmployeeType(String employeeType) { this.employeeType = employeeType; } }
Since the SRP (Single Responsibility Principle) recommends only one reason for change in the class, we will have to make some modifications in the Employee class
Changes by SRP
Now, there is only one reason for which change can occur in Employee class
Reason for change: Change in Employee attribute
/** * Employee class has details of employee */ public class Employee { private String employeeId; private String employeeName; private String employeeAddress; private String contactNumber; private String employeeType; public void save() { new EmployeeRepository().save(this); } public void calculateTax() { new TaxCalculator().calculateTax(this); } public String getEmployeeId() { return employeeId; } public void setEmployeeId(String employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getEmployeeAddress() { return employeeAddress; } public void setEmployeeAddress(String employeeAddress) { this.employeeAddress = employeeAddress; } public String getContactNumber() { return contactNumber; } public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber; } public String getEmployeeType() { return employeeType; } public void setEmployeeType(String employeeType) { this.employeeType = employeeType; } }
Also, there is only one reason for which change can occur in EmployeeRepository class
Reason for change: Change in database
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class EmployeeRepository { public void save(Employee employee) { String insert = MyUtil.serializeIntoString(employee); Connection connection = null; Statement statement = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc://mysql://localhost:8080/MyDb", "root", "password"); statement = connection.createStatement(); statement.execute("insert into employee values (" + insert + ")"); } catch (Exception e) { e.printStackTrace(); } } }
Finally, there is only one reason for which change can occur in TaxCalculator class
Reason for change: Change in Tax calculation
public class TaxCalculator { public void calculateTax(Employee employee) { if (employee.getEmployeeType().equals("fulltime")) { // tax calculation for full time employee } else if (employee.getEmployeeType().equals("contract")) { // tax calculation for contract type employee } } }
Note: All the 3 classes now follow the Single Responsibility Principle, thus following both definitions
When creating classes or any software component keep in mind: Aim for high cohesion and loose coupling
Every software component should have one and one responsibility only and
Every software component should have one and only one reason to change
The above is the detailed content of Single Responsibility Principle. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
