当业务逻辑发生变化时,如何尊重开放封闭原则?

发布于 2024-09-15 23:29:00 字数 163 浏览 2 评论 0原文

我们正在对我们的系统进行一些重大更改,我想知道实现这些新业务逻辑规则的最佳方法,尊重 SOLID 原则:

开放/封闭原则说“开放用于扩展,但关闭用于修改”好吧,但是如何我可以修改吗?我的意思是,我不想保留旧的业务逻辑,在我看来“扩展”主要意味着“添加代码”而不是“更改代码”,那么我理解错了什么?

We are doing some big changes in our system and I'd like to know the best way to implement these new business logic rules, respecting SOLID principles :

Open / Closed principles says "Open for extension, but close for modification" ok, but how can I do modification ? I mean, I don't want to keep the old business logic, and in my mind "extension" mainly means "adding code" not "changing code" , so what did I understood wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海拔太高太耀眼 2024-09-22 23:29:00

Open/Closed 背后的想法是,如果您需要不同的业务逻辑,则需要创建一个新的业务逻辑类。

然而,这样做的主要动机是您不想影响现有的代码、重新测试、再次签核等。如果您使用的业务逻辑发生了根本性的变化,那么您将更改对的所有引用一个新对象并废弃旧对象,在这种情况下重新打开对象进行修改是可以接受的。关键是(1)无论如何你都需要重新测试所有代码,(2)旧对象不会在任何地方使用。

哈特哈,
詹姆斯

The idea behind Open/Closed is that if you need different business logic, you'll need to create a new business-logic class.

However, the primary motivation for this is that you don't want to impact existing code that's out there, retest, get signoff again, etc. If the business logic you're using has fundamentally changed, and you would be changing all references to a new object and obsoleting the old one, it would be acceptable in this case to re-open the object to modification. The key being (1) you're going to need to retest all the code anyway, and (2) the old object would not be used anywhere.

HTH,
James

千秋岁 2024-09-22 23:29:00

两个大问题:

我们在这里谈论什么样的变化?

您有什么样的现有代码?它是否已经符合任何 SOLID 原则?

假设我们需要对设计良好的现有应用程序进行一些更改。也许考虑一下工资单应用程序。我们可能有一个Interafce(这只是一个人为的示例)

 public Interface EmployeeContract {         
      public int computeSalaryForMonth(int month);
 }

并且我们有实现:

 public class SalesContract implements EmployeeContract {
      public int computeSalaryForMonth(int month){
               // computation involving sales figures and base salary
      }
 }

  public class HourlyContract implements EmployeeContract {
      public int computeSalaryForMonth(int month){
               // computation involving hours worked and overtime payments
      }
 }

现在应用程序的所有其他部分都是根据接口进行编码的。

我们现在可以添加新类型的合约,而无需更改任何现有代码,我们愿意进行扩展并添加可能相当复杂的新业务逻辑。

但是那是因为最初的设计师预料到了这种变化,添加了新的每月合同类型。如果我们想雇用每周领取工资的员工,那不是很好吗?现在我们的界面不合适,我们需要改变它,并且效果会波及其他代码。

实际上,软件不会对任意业务逻辑的轻松更改开放,并且实际上尝试预先实现灵活性需要花费大量精力。但是请注意,即使在我的示例中,接口并不像我们现在需要的那么灵活,因为接口是耦合点,所以很容易识别需要更改的代码部分。

摘要:根据您的情况:

  1. 了解现有代码的结构以及它的灵活性点。接受某些代码需要更改的事实,并且业务中的重大更改可能需要对代码进行重大更改也就不足为奇了。
  2. 结构通常有助于可维护性,因此在添加新代码时要注意结构。通过对接口进行编码,在代码中提供“防火墙”,在完成工作和开放扩展之间取得平衡。

Two big questions:

What kind of changes are we talking we about here?

What kind of existing code do you have? Is it already conforming to any SOLID principles?

Let's suppose we need to make some changes to a well-designed existing application. Consider perhaps a payroll application. We might have an Interafce (this is just a contrived example)

 public Interface EmployeeContract {         
      public int computeSalaryForMonth(int month);
 }

and we have implementations:

 public class SalesContract implements EmployeeContract {
      public int computeSalaryForMonth(int month){
               // computation involving sales figures and base salary
      }
 }

  public class HourlyContract implements EmployeeContract {
      public int computeSalaryForMonth(int month){
               // computation involving hours worked and overtime payments
      }
 }

Now all the other parts of the application are coded in terms of the Interface.

We can now add new kinds of contract without changing any existing code, we are open for extension and adding possibly quite complex new business logic.

BUT that's because the original designer anticipated that kind of change, adding new monthly contract types. Not so good if we wanted to take on employees who are paid weekly? Now our interface is not suitable, we need to change it, and the effects will ripple through other code.

Realistically, software will not be open to painless change of arbitrary business logic, and indeed trying to be flexible up front can absorb lots of effort. However do notice that even though in my example the Interface is not quite as flexible as we now need, because the Interface is the coupling point it's vey easy to identify the portions of the code that need to be changed.

Summary: in your situation:

  1. Understand the structure of the existing code and where it has points of flexibility. Accept that some code will need to change, and that it's unsurprising that significnat changes in the business may require significant changes in the code.
  2. Structure generally helps with maintainability, so as you add new code pay attention to structure. Provide "firewalls" in your code by coding to Interfaces, striking a balance between getting the job done and being open to extension.
谁把谁当真 2024-09-22 23:29:00

开放-封闭原则意味着您不必更改当前的代码,因此它对于修改是关闭的,但为了满足新的需求,您需要使用子类、实现接口、设计模式来扩展代码,以便您的代码对此活动是开放的。当然,在重构时你会面临需要更改代码的情况,但你必须以某种方式来满足 OSP。

例如,您可以创建工厂方法,而不是创建分散在代码中的类似对象的新实例。当新的需求添加一个新对象时,您的代码将关闭以进行修改(代码中没有新实例),但将打开以进行扩展(扩展工厂方法)。

Open-closed principles means that you don't have to change your present code so it's CLOSED for modification but to fulfil new requirements your need to extend the code using subclasses, implementing interfaces, desing patterns so that your code is OPEN for this activities. of cause while refactoring you will face that you need to CHANGE code but you have to do it a way to saticfy OSP.

for instance instead of creting new instaces of similar object scattered around the code you may to create a Factory Method. When new requiremet comes to add one new object your code will be CLOSED for modification (no new instances in the code) but OPEN for extension (extend Factory Method).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文