04-20.eri-test S.O.L.I.D:开放/封闭原则

What is S。Ø。大号.一世.D顺便问一下?

As per Wiki:

在面向对象的计算机编程中,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代表 -

在本文中,我将介绍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();
  }
}

这是减少冗长的UML图:
UML diagram for ocp principle

开始了! 如果我们有一个新的要求来添加不同类型的投资,我们可以进一步创建一个新类,实现Investment界面,我们就完成了。 无需修改任何现有代码。

TL;DR

始终牢记使您的代码为扩展而打开,为修改而关闭。 这将使代码的维护变得如此容易。

本文中的示例使用了组合设计模式(策略模式)来实现OCP,但也可以通过使用继承来实现。

和平!
如果您有任何疑问或反馈,请随时在下面发表评论。

from: https://dev.to//linuxnerd/s-o-l-i-d-open-closed-principle-2lb0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值