接口隔离原则 - 对接口进行编程
我正在阅读有关 SOLID 和其他设计原则的内容。我认为 ISP 与“对接口编程,而不是实现”相同。但看起来这些是不同的原理?
有区别吗?
I was reading about SOLID and other design principles. I thought ISP was the same as "Program to an interface, not an implementation". But it looks like these are different principles?
Is there a difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Robert Martin 在他的《UML for Java Programmers》一书中对接口隔离原则(ISP)有很好的解释。基于此,我不认为 ISP 是一个“专注于”逻辑上、连贯的一组事物的接口。因为,这是不言而喻的;或者,至少应该不言而喻。每个类、接口或抽象类都应该这样设计。
那么,什么是ISP?让我用一个例子来解释一下。假设,你有一个类A和一个类B,类B是类A的客户端。假设,类A有十个方法,其中只有两个被B使用。现在,B是否需要知道A的所有十个方法?可能不是——信息隐藏的原理。你暴露的越多,你创造的耦合机会就越多。因此,您可以在两个类之间插入一个接口(称为 C)(隔离)。该接口只会声明 B 使用的两个方法,并且 B 将依赖于该接口,而不是直接依赖于 A。
所以现在,
将变成
This,阻止 B 知道更多的信息。
Robert Martin has a very good explanation of Interface segregation principle (ISP), in his book "UML for Java Programmers". Based on that, I don't think ISP is about an interface being "focused" on one logical, coherent group of things. Because, that goes without saying; or, at least it should go without saying. Each class, interface or abstract class should be designed that way.
So, what is ISP? Let me explain it with an example. Say, you have a class A and a class B, which is the client of class A. Suppose, class A has ten methods, of which only two are used by B. Now, does B need to know about all ten methods of A? Probably not - the principle of Information hiding. The more you expose, the more you create the chance for coupling. For that reason, you may insert an interface, call it C, between the two classes (segregation). That interface will only declare the two methods that are used by B, and B will depend on that Interface, instead of directly on A.
So now,
will become
This, prevents B from knowing more than it should.
ISP 专注于每个接口代表一种离散且内聚的行为的理念。
也就是说,对象应该执行的每个逻辑事物组都将映射到单个特定接口。一个类可能想要做几件事,但每件事都会映射到代表该行为的特定接口。这个想法是每个界面都非常集中。
ISP is focused on the idea of each interface representing one discrete and cohesive behavior.
That is, each logical group of things an object should do would map to a single specific interface. A class might want to do several things, but each thing would map to a specific interface representing that behavior. The idea is each interface is very focused.
假设您有一个胖接口,其中包含许多要实现的方法。
任何实现该胖接口的类都必须提供所有这些方法的实现。有些方法可能不适用于该具体类。但在没有接口隔离原则的情况下,它仍然必须提供实现。
让我们看一下没有接口隔离的示例代码。
输出:
注释:
Shape
是一个通用胖接口,其中包含所有Shape
实现(如Rectangle
、)所需的方法圆形
和方形
。但在各自的 Shape 子项中只需要一些方法在没有隔离的情况下,所有 Shape 都实现了整个 fat 接口:Shape。
如果我们将代码更改如下,我们可以按照接口隔离原则实现相同的输出。
注意:
现在诸如
Rectangle
、Square
和Circle
之类的单个 Shape 只实现了必需的接口,并摆脱了未使用的方法。Assume that you have one fat interface with many methods to be implemented.
Any class, that implements that fat interface has to provide implementation for all these methods. Some of the methods may not be applicable to that concrete class. But still it has to provide implementation in absence of interface segregation principle.
Let's have a look at example code in absence of Interface segregation.
output:
Notes:
Shape
is a general purpose fat interface, which contains methods required for allShape
implementations likeRectangle
,Circle
andSquare
. But only some methods are needed in respective Shape childsIn absence of segregation, all Shapes have implemented entire fat interface : Shape.
We can achieve same output with interface segregation principle if we change the code as follows.
Notes:
Now individual Shapes like
Rectangle
,Square
andCircle
have implemented only required interfaces and got rid of un-used methods.同意上面两个答案。举个上面 TrueWill 代码味道的例子,你不应该发现自己这样做:
Agree with both the answers above. Just to give an example of TrueWill's code smell above, you shouldn't find yourself doing this:
IWorker接口:
开发人员类:
机器人类别:
有关更完整的示例,请访问 这里。
IWorker Interface:
Developer Class :
Robot Class:
For a more complete example go here.
这是该原则的一个真实示例(使用 PHP)
问题陈述:
我希望各种形式的内容都具有与之相关的评论/讨论。该内容可能是从论坛主题、新闻文章、用户个人资料到对话式私人消息的任何内容。
架构
我们需要一个可重用的
DiscussionManager
类,它将Discussion
附加到给定的内容实体。然而,上述四个例子(以及更多)在概念上都是不同的。如果我们希望 DiscussionManager 使用它们,那么所有四个以上都需要有一个共享的通用接口。DiscussionManager
没有其他方法可以使用它们,除非您希望您的参数裸露(例如不进行类型检查)。解决方案:使用以下方法的
Discussable
接口:attachDiscussion($topic_id)
detachDiscussion()
getDiscussionID()
然后
DiscussionManager
可能如下所示:这样,
DiscussionManager
就不会关心它使用的各种内容类型的任何不相关行为。它只关心它需要的行为,无论这些行为与什么相关。因此,通过为您想要讨论的每种内容类型提供一个可讨论的接口,您正在使用接口隔离原则。这也是抽象基类不是一个好主意的情况的一个很好的例子。论坛主题、用户个人资料和新闻文章在概念上根本不是同一件事,因此试图让它们继承讨论行为会导致与不相关的父级的奇怪耦合。使用代表讨论的特定接口,您可以确保您想要讨论的实体与将管理这些讨论的客户端代码兼容。
就其价值而言,这个示例也可能是在 PHP 中使用 Traits 的一个很好的候选者。
Here's a real-world example of this principle (in PHP)
Problem Statement:
I want various forms of content to have comments/discussion associated with them. That content might be anything from a forum topic, to a news article, to a user's profile, to a conversation-style private message.
Architecture
We will want a re-usable
DiscussionManager
class which attaches aDiscussion
to a given content entity. However, the above four examples (and many more) are all conceptually different. If we want theDiscussionManager
to use them, then all four+ need to have one common interface that they all share. There is no other way forDiscussionManager
to use them unless you want to your arguments to go naked (e.g. no type checking).Solution:
Discussable
interface with these methods:attachDiscussion($topic_id)
detachDiscussion()
getDiscussionID()
Then
DiscussionManager
might look like this:This way,
DiscussionManager
does not care about any of the unrelated behaviors of the various content types that it uses. It ONLY cares about the behaviors it needs, regardless of what those behaviors are associated with. So by giving each content type that you want to have discussions for, aDiscussable
interface, you are using the interface segregation principle.This is also a good example of a situation where an abstract base class is not a good idea. A forum topic, user profile, and news article aren't even remotely conceptually the same thing, thus trying to get them to inherit the discussion behaviors leads to strange coupling to an unrelated parent. Using a specific interface that represents discussions, you can makes sure that the entities you want to have discussions, are compatible with the client code that will be managing those discussions.
This example might also be a good candidate for usage of Traits in PHP, for what it's worth.