When are interfaces needed?

前端 未结 15 1594
日久生厌
日久生厌 2020-12-13 00:56

(In the context of .NET for what its worth)

I tend to not use inheritance and rarely use interfaces. I came across someone who thinks interfaces are the best thing

15条回答
  •  难免孤独
    2020-12-13 01:26

    Interfaces make the most sense to me in the context of dependency injection and IoC frameworks. I like the idea that you can define a set of behaviors (methods) and "guarantee" those behaviors through the implementation of an interface. Now you can plug whole new functions into an existing system with a single assembly and a config file update.

    Effective design of interfaces does require a good deal of forward planning, and I find they are most useful in the context of large systems and frameworks. When they're useful they're really useful. A few of my favorite:

    • IComparable (YOU decide how your objects compare against each other)
    • IQueryable (LINQ anyone?)
    • IDisposable (keep your using statement handy)

提交回复
热议问题