The practice of coding against interfaces is already a well recognised
best practice for software development, one of the advantages of this is that
it helps loosen the dependencies between a class and the various other classes
it uses. An application where this can be extremely useful is when unit testing
your code, by loosening the dependencies of a class it becomes possible to test
a class in isolation.
A good example of how to use these would be in data access classes, the
real data access class would undoubtedly make calls to a physical database
which would required configuration etc. However when testing a class that
depends on the data access class we don’t want this dependency. Therefore the
solution is simple. Define an interface, make both he real and mock classes
implement this interface, the only difference is that the real implementation would
go ahead and make calls to insert, update and delete from a database the mock
will return only hard coded values so that you know exactly what to expect when
testing a class that uses the data access components.
There are various frameworks that help to facilitate the creation of
mock objects such as NMock, these are slightly more complex and give some extra
features but the principle of coding to interfaces must be adhered to in order
to take advantage of such frameworks.
To see an example of using mock objects see my post on PrivateObject,
here I have simply used standard interfaces as this proved effective enough to
conduct the testing. If your situation requires something more you can always
look into NMock which is a free download.