单一责任原则
每个软件组件都应该只有一个且一个职责
软件组件可以是类、方法或模块
例如,瑞士军刀是一种多用途工具,违反了软件开发的单一责任原则,相反,刀是遵循单一责任的一个很好的例子(因为与瑞士军刀不同,它只能用于切割可用于切割、打开罐头、作为万能钥匙、剪刀等)
由于无论是现实世界还是软件开发,变化都是不变的,单一职责原则的定义也会随之变化
每个软件组件都应该有一个且仅有一个更改的理由
下面的 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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本启动Spring...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

Redis缓存方案如何实现产品排行榜列表的需求?在开发过程中,我们常常需要处理排行榜的需求,例如展示一个�...
