加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oop – 在SOLID中,SRP和ISP之间的区别是什么? (单一责任原则和

发布时间:2020-12-14 01:02:50 所属栏目:百科 来源:网络整理
导读:SOLID“接口隔离原理”与“单一责任原则”有何不同? 维基百科entry for SOLID says那个 ISP splits interfaces which are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest
SOLID“接口隔离原理”与“单一责任原则”有何不同?

维基百科entry for SOLID says那个

ISP splits interfaces which are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them

然而,对我来说听起来只是将SRP应用于接口和类。毕竟,如果一个接口只负责一个概念性的事情,那么你不能再进一步破坏它。

我缺少某些东西,还是使用SRP进行ISP冗余?如果不是,那么ISP是什么意味着SRP没有?

SRP告诉我们,您应该只在模块中承担单一责任。

ISP告诉我们,你不应该被迫面对比实际需要的更多的东西。如果要使用界面I中的print()方法,则不需要为此实例化一个SwimmingPool或DriveThru类。

更具体地说,直到现在,他们对同样的想法是不同的看法 – SRP更侧重于设计者侧的观点,而ISP更侧重于客户端的观点。所以你基本上是对的

一切都来自

The ISP was first used and formulated by Robert C. Martin when doing
some consulting for Xerox. Xerox had created a new printer system that
could perform a variety of tasks like stapling a set of printed papers
and faxing. The software for this system was created from the ground
up and performed its tasks successfully. As the software grew,making
modification became more and more difficult so that even the smallest
change would take a redeployment cycle to an hour. This was making it
near impossible to continue development. The design problem was that
one main Job class was used by almost all of the tasks. Anytime a
print job or a stapling job had to be done,a call was made to some
method in the Job class. This resulted in a huge or ‘fat’ class with
multitudes of methods specific to a variety of different clients.
Because of this design,a staple job would know about all the methods
of the print job,even though there was no use for them.

所以

The solution suggested by Martin is what is called the Interface
Segregation Principle today. Applied to the Xerox software,a layer of
interfaces between the Job class and all of its clients was added
using the Dependency Inversion Principle. Instead of having one large
Job class,a Staple Job interface or a Print Job interface was created
that would be used by the Staple or Print classes,respectively,
calling methods of the Job class. Therefore,one interface was created
for each job,which were all implemented by the Job class.

@ http://en.wikipedia.org/wiki/Interface_segregation_principle#Origin

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读