Useless interfaces

后端 未结 25 1616
南笙
南笙 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:27

    It bothers me too when people make an Interface, an Abstract, and an actual implementation for every class, even if there will never be more than one, and the 3 files are all nearly empty.

    However, big uses would be:

    1. Future expansion / enhancements placeholder

    2. Easy to implement Inversion of Control / Dependency Injection

    3. Easy to implement mocks for unit testing.

    *Edit:

    I noticed you have Spring in your tags too. If using Spring, then #2 above is probably the biggie. Its easier to design classes that expect interfaces, and you can swap out actual implementations of the interface in the Spring configuration (Dependency Injection).

提交回复
热议问题