Useless interfaces

后端 未结 25 1637
南笙
南笙 2020-12-14 01:42

Why would you ever use an interface if you are only going to have one implementation of it?

25条回答
  •  清歌不尽
    2020-12-14 02:44

    I often do this and for several reasons:

    1) It allows you to specify which features of a class are actually being used outside the class. This tells you which features you always need to support as long as that interface remains unchanged. This allows you to have other public methods in that class for whatever reason (to support other interfaces or for other objects that have an instance of the object and refer to it by the actual class name as opposed to through the interface).

    2) It also trims down the list provided through intellisense to only the features supported by the interface.

    3) And as stated above, it's useful for unit testing and mocking frameworks.

提交回复
热议问题