开放-封闭原则和编程到接口是否相同?

huangapple go评论50阅读模式
英文:

Is the Open-Closed SOLID principle the same as Coding to an Interface?

问题

"Does open-for-extension and closed-for-modification mean code-to-an-interface?

If I code to an interface so that future concrete implementations can be introduced by implementing the interface, and we create new classes without touching existing logic, does it achieve the same goal we try to address using the open-for-extension and closed-for-modification SOLID principle?"

开放扩展和封闭修改是否意味着按接口编码?

如果我按接口编码,以便将来可以通过实现接口引入具体的实现,并且我们创建新的类而不触及现有逻辑,是否能够实现与使用开放扩展和封闭修改的SOLID原则所要解决的相同目标?

英文:

Does open-for-extension and closed-for-modification mean code-to-an-interface?

If I code to an interface so that future concrete implementations can be introduced by implementing the interface, and we create new classes without touching existing logic, does it achieve the same goal we try to address using the open-for-extension and closed-for-modification SOLID principle?

答案1

得分: 0

我会说,编写代码以符合接口解决了开闭原则(OCP),但还有其他方法可以编写以符合接口。

编写以符合接口也可以意味着编写以符合抽象类,比如 InputStream。

此外,使用 CDI 可以允许将 bean 设置为关闭以进行修改并开放以进行扩展。

这意味着您不一定要使用接口,但为了符合 OCP,您可以使用接口。

如果我编写以符合接口 (...) 是否可以实现 (...) SOLID 原则?

是的,您遵循了SOLID原则。但接口的实现者可能没有遵循。

通常,接口被分为方法签名和javadoc。方法签名指定了您在技术上可以做什么,而javadoc应该指定实现应该做什么。无论哪种方式,签名和文档都是硬性规定,违反签名或文档的实现通常应报告为错误。

因此,您可以使用现有接口来满足常见需求,或者针对特定需求创建新接口。如果创建新接口,您可能需要考虑接口分离原则,这也是SOLID原则的一部分。

在设计此接口时,您必须对实现者的需求持开放态度。例如,如果接口如下所示:

Date readDate(InputStream in);

可能会出现一些实现者的需求,您以前无法预见:

  1. 在流关闭时应添加一个 IOException。
  2. 应添加一个 Charset 参数,因为字符集是ASCII,这意味着位长度为7位,而不是8位。
  3. 应添加时区,因为Date使用服务器的时区,而流的来源可能位于不同的时区。
  4. 应添加Locale,因为年/月/日格式(英国)可能在不同的区域设置中为年-月-日格式(德国)。

这些需求可能会在您编写以符合接口之后出现。我会说,这些对接口的需求迫使您修改了已关闭的接口。您可以说,是的,这是一种违反关闭修改的修改。但这些“对接口的需求”实际上不需要修改您的编写以符合接口的行为。

英文:

I would say that code-to-an-interface solves the open-close-principle (OCP) but there are other ways to code-to-an-interface.

Code-to-an-interface could also mean to code to an abstract-class like InputStream.

Also the use of CDI could allow beans to be closed-for-modification and open-for-extensions.

This means that you must not use interfaces but you can in order to use OCP.

> If I code to an interface (...) does it achieve (...) SOLID principle?

Yes, you follow the SOLID principle. But implementors of the interface might not.

Usually the interface is separated into a method-signature and the javadoc. The method-signature specifies what you technically could do and the javadoc should specify what the implementation should do. Both ways, signature and doc are hard facts and a implementation that violates either the signature or the javadoc mostly should reported as a bug.

So you can either use a existing interface for common requirements or a new interface for specific requirements. If you create a new interface you might think about the interface-segregation-principle what also is part of the SOLID-principle-collection.

If you design this interface you must be open for requirements of the implementor. In example if the interface is like this:

Date readDate(InputStream in);

There might be some requirements from the implementors you could never have foreseen:

  1. You should add a IOException in the case the stream is closed.
  2. You should add a Charset-Parameter because the charset is ASCII what means the bitlength is 7 bit, not 8 bit.
  3. You should add a Timezone because Date use the timezone of the server whearat the stream sources from a different timezone.
  4. You should add Locale because the year/month/day format (UK) could be year-month-day format (GER) in a different locale.

Theese requirements might appear after you coded-to-an-interface. I would say those requirements to the interface forces you to modify your code-to-an-interface that are closed. You could say, yes this is a modification that violates against closed-for-modifications. But none of theese "requirements to the interface" really need to modify the behaviour of your code-to-an-interface.

huangapple
  • 本文由 发表于 2020年1月3日 21:27:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定