Does implementing multiple interfaces violate Single Responsibility Principle?

前端 未结 3 2111
轻奢々
轻奢々 2021-02-08 01:22

From Wikipedia:

Single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-08 01:37

    "Single Responsibility" depends on the level of abstraction. For example, a complex system, considering it at a system level, may have one responsibility. For instance, a TV system's responsibility is to show video picture. At the next, lower level, that system is made of sub-systems, monitor, power unit, etc. At this level, each of these units have their own responsibilities.

    In the same way, a class, at one level may be considered to have a single responsibility. But, at a lower level, it may have other constituent modules (classes, interfaces etc) that perform parts of its job. For example, a Student class's responsibility is to represent a student abstraction. It may however have another unit (a class) that represents student's address.

    In this way, using multiple interfaces do not by itself violate object-oriented principles.

提交回复
热议问题