One stumbles upon this phrase when reading about design patterns.
But I don't understand it, could someone explain this for me?
One stumbles upon this phrase when reading about design patterns.
But I don't understand it, could someone explain this for me?
It means that you should try to write your code so it uses an abstraction (abstract class or interface) instead of the implementation directly.
Normally the implementation is injected into your code through the constructor or a method call. So, your code knows about the interface or abstract class and can call anything that is defined on this contract. As an actual object (implementation of the interface/abstract class) is used, the calls are operating on the object.
This is a subset of the
Liskov Substitution Principle
(LSP), the L of theSOLID
principles.An example in .NET would be to code with
IList
instead ofList
orDictionary
, so you could use any class that implementsIList
interchangeably in your code:Another example from the Base Class Library (BCL) is the
ProviderBase
abstract class - this provides some infrastructure, and as importantly means all provider implementations can be used interchangeably if you code against it.