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

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...