As long as your code looks as simple like this
void ExampleFunc()
{
ICustomer oCustomer = new Customer();
oCustomer.Method1OfICustomer();
oCustomer.Method2OfICustomer();
// ...
}
there is no semantic difference - you can exchange "ICustomer" by "Customer", and the behaviour will stay identical. In this example, however, it could fulfill already some documentary purposes. The first line may express the design decision of the programmer to avoid adding calls like
oCustomer.MethodNotPartOfICustomer();
in the future, but in fact, when this kind of call is needed later on, one could also change the "new" line, or add the missing method to the ICustomer
later.
In fact, the use of interfaces, as the term implies, will start to make more sense when there is some real "interfacing" involved - interfacing between functions, classes, components. For example, the function
void ExampleFunc(ICustomer oCustomer)
{
oCustomer.Method1OfICustomer();
oCustomer.Method2OfICustomer();
// ...
}
is probably more generic than
void ExampleFunc(Customer oCustomer)
{
oCustomer.Method1OfICustomer();
oCustomer.Method2OfICustomer();
// ...
}
since it will work with any class implementing the ICustomer
interface, not just Customer
. For example, for testing purposes one could think of passing a "CustomerMock" into the function, something you cannot do with the second variant.