What's the big deal about interfaces?
Once you define the contract, you can swap out implementations without worrying about breaking the rest of your code.
Consider the situation where you have poor performing code that is making use of a List<T>
in .NET. If you use the hard implementation of List<T>
, there's a good chance you're going to break more code by changing the implementation.
If you were using IList<T>
or IEnumerable<T>
you would be able to swap List<T>
for LinkedList<T>
(or anything implementing your chosen interface) and fix the issue in one spot rather than having to touch all of your code.
In the end...it's about about Polymorphism.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…