In SOLID, what is the distinction between SRP and ISP? (Single Responsibility Principle and Interface Segregation Principle)

前端 未结 4 1670
情歌与酒
情歌与酒 2021-02-02 06:05

How does the SOLID \"Interface Segregation Principle\" differ from \"Single Responsibility Principle\"?

The Wikipedia entry for SOLID says that

IS

4条回答
  •  借酒劲吻你
    2021-02-02 06:08

    SRP is concerned with what a module does, and how it is done, disallowing any mix of abstraction levels. Basically, as long as a component can be extensively defined with a single sentence, it will not break SRP.

    On the other hand ISP is concerned with how a module should be consumed, whether it makes sense to consume just part of the module, while ignoring some aspect.

    As an example of a code that keeps the spirit or SRP, but can break ISP is the Facade pattern. It has a single responsibility, "providing simplified access to a larger subsystem", but if the underlying subsystem needs to expose wildly different thinks, it does break ISP.

    That said, usually when a piece of code breaks a SOLID principle, it often breaks the whole lot. Concrete examples that break a specific principle, while preserving the rest are rare in the wild.

提交回复
热议问题