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

前端 未结 4 1685
情歌与酒
情歌与酒 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:14

    Robert Martin tweeted the following on May 16, 2018.

    ISP can be seen as similar to SRP for interfaces; but it is more than that. ISP generalizes into: “Don’t depend on more than you need.” SRP generalizes to “Gather together things that change for the same reasons and at the same times.”

    Imagine a stack class with both push and pop. Imagine a client that only pushes. If that client depends upon the stack interface, it depends upon pop, which it does not need. SRP would not separate push from pop; ISP would.

提交回复
热议问题