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

前端之家收集整理的这篇文章主要介绍了oop – 在SOLID中,SRP和ISP之间的区别是什么? (单一责任原则和接口隔离原则)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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

猜你在找的设计模式相关文章