每个软件组件都应该只有一个且一个职责
软件组件可以是类、方法或模块
例如,瑞士军刀是一种多用途工具,违反了软件开发的单一责任原则,相反,刀是遵循单一责任的一个很好的例子(因为与瑞士军刀不同,它只能用于切割可用于切割、打开罐头、作为万能钥匙、剪刀等)
由于无论是现实世界还是软件开发,变化都是不变的,单一职责原则的定义也会随之变化
每个软件组件都应该有一个且仅有一个更改的理由
下面的 Employee 类发生变化的原因有以下三个
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; } }
由于SRP(单一职责原则)建议在类中只有一个更改原因,因此我们必须在Employee类中进行一些修改
SRP 变化
现在,Employee 类中发生变化的原因只有一个
变更原因:员工属性变更
/** * 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; } }
此外,EmployeeRepository 类中发生更改的原因只有一个
更改原因:数据库更改
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(); } } }
最后,TaxCalculator 类中发生更改的原因只有一个
变更原因:税务计算变更
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 } } }
注意:所有 3 个类现在都遵循单一职责原则,因此遵循两个定义
创建类或任何软件组件时请记住:以高内聚和松散耦合为目标
每个软件组件应该只有一个和一个职责和
每个软件组件都应该有一个且仅有一个更改的理由
以上是单一责任原则的详细内容。更多信息请关注PHP中文网其他相关文章!