每個軟體元件應該只有一個且一個職責
軟體元件可以是類別、方法或模組
例如,瑞士軍刀是一種多用途工具,違反了軟體開發的單一責任原則,相反,刀是遵循單一責任的一個很好的例子(因為與瑞士軍刀不同,它只能用於切割可用於切割、打開罐頭、作為萬能鑰匙、剪刀等)
由於無論是現實世界或軟體開發,變化都是不變的,單一職責原則的定義也會隨之改變
每個軟體元件都應該有且只有一個更改的理由
下面的 Employee 類別中可能發生變化的原因有 3 個原因
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 個類別現在都遵循單一職責原則,因此遵循兩個定義
在建立類別或任何軟體元件時請記住:以高內聚和鬆散耦合為目標
每個軟體元件應該只有一個和一個職責和
每個軟體元件都應該有且僅有一個更改的理由
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3