What is S。Ø。大号.一世.D顺便问一下?
在面向对象的计算机编程中,SOLID是五个设计原则的助记词缩写,旨在使软件设计更易于理解,灵活和可维护。
SOLID is an acronym for 5 important design principles when doing OOP. 它是由Robert C首次引入的。. Martin (Uncle Bob), in his 2000 paper Design Principles和Design Patterns.
SOLID代表 -
- S - Single Responsibility Principle
- O - Open/Closed Principle
- L - Liskov's Substitution Principle
- I - Interface Segregation Principle
- D - Dependency Inversion Principle
在本文中,我将介绍O-开/关原理.
注意-示例将使用Java,但适用于任何OOP语言。
O - Open/Closed Principle (OCP)
开放/封闭原则(OCP)是SOLID原则,其中指出
软件实体(类,模块,功能等)应打开以进行扩展,但应关闭以进行修改。
简而言之,当客户或产品所有者提出变更请求时,我们应努力编写无需修改即可扩展的代码。
Anti OCP usage
我们首先来看一个打破OCP原则的例子。
// 一般投资.java
public class 一般投资 {
public Double initialAmount;
public int year;
public Double tax;
public 一般投资(Double initialAmount, int year, Double tax) {
this.initialAmount = initialAmount;
this.year = year;
this.tax = tax;
}
}
//投资经理.java
public final class 投资经理 {
public Double calculateReturn(General投资 investment) {
return investment.initialAmount + (investment.year * investment.initialAmount * investment.tax);
}
}
所以,我们有一个General投资
类和投资经理
类。 的唯一目的投资经理
是计算类型的回报GeneralInvestment
.
更进一步,我们有一个功能请求来计算收益先进投资
。 让我们添加一些代码来完成此操作。
//先进投资.java
public class 先进投资 extends GeneralInvestment {
public Double incremental;
public int period;
public AdvancedInvestment(Double initialAmount, int year, Double tax, Double incremental, int period) {
super(initialAmount, year, tax);
this.incremental = incremental;
this.period = period;
}
}
现在,我们有AdvancedInvestment
到位。 让我们更新我们的投资经理
类以处理新案件。
public final class InvestmentManager {
public Double calculatereturn(GeneralInvestment investment) {
if (investment instanceof AdvancedInvestment) {
AdvancedInvestment advancedInvestment = (AdvancedInvestment) investment;
return advancedInvestment.initialAmount
+ (advancedInvestment.year * advancedInvestment.initialAmount * advancedInvestment.tax)
+ (advancedInvestment.period * advancedInvestment.incremental * advancedInvestment.tax);
} else {
return investment.initialAmount + (investment.year * investment.initialAmount * investment.tax);
}
}
}
这可行!
但是,它违反了OCP,因为InvestmentManager
类是
- 封闭延长
- 开放修改
OCP in action
让我们尝试在不破坏OCP的情况下实现上述方案。
第1步:创建一个界面
// Investment.java
interface Investment {
public Double calculateReturn();
}
第2步:更新资料GeneralInvestment
and AdvancedInvestment
通过实施课程Investment
接口
//GeneralInvestment.java
public class GeneralInvestment implements Investment {
public Double initialAmount;
public int year;
public Double tax;
public GeneralInvestment(Double initialAmount, int year, Double tax) {
this.initialAmount = initialAmount;
this.year = year;
this.tax = tax;
}
public Double calculateReturn() {
return initialAmount + (year * initialAmount * tax);
}
}
//AdvancedInvestment.java
public class AdvancedInvestment implements Investment {
public Double incremental;
public int period;
public GeneralInvestment investment;
public AdvancedInvestment(Double initialAmount, int year, Double tax, Double incremental, int period) {
this.incremental = incremental;
this.period = period;
this.investment = new GeneralInvestment(initialAmount, year, tax);
}
public Double calculateReturn() {
return investment.initialAmount + (investment.year * investment.initialAmount * investment.tax)
+ (period * incremental * investment.tax);
}
}
第三步:最后更新InvestmentManager
class
//InvestmentManager.java
public final class InvestmentManager {
public Double calculateReturn(Investment investment) {
return investment.calculateReturn();
}
}
开始了! 如果我们有一个新的要求来添加不同类型的投资,我们可以进一步创建一个新类,实现Investment
界面,我们就完成了。 无需修改任何现有代码。
TL;DR
始终牢记使您的代码为扩展而打开,为修改而关闭。 这将使代码的维护变得如此容易。
本文中的示例使用了组合设计模式(策略模式)来实现OCP,但也可以通过使用继承来实现。
和平!
如果您有任何疑问或反馈,请随时在下面发表评论。