program to an interface not implementation
We all heard of this before from the the gang f four and other locations. Maybe lack of sleep is confusing my brain...here is my question
I have a c# user control. have 2 methods add and subtract.
case1:
they are declared as public methods so when the user control is instantiated, say in a form, one can do
Code:
usercontrol.add(1,1);
and
Code:
usercontrol.subtract(100,1);
The form where the control is instantiated does not know how the add and subtract methods are implemented. would that be a fair statement?
case2:
same as case1 except that add and subtracts methods are exposed via interfaces and one gets to them in usual manner
the question now is that in either of the 2 cases the client has no ideas on how the methods are implemented and in case of a user control why would i choose to go with interfaces and does this statement 'program to an interface not implementation' hold valid??
Re: program to an interface not implementation
In one case the calling program "knows" that your UserControl class has implemented the methods. It created the object, it knows it's type.
When programming to a pure interface, the actual object type is not even known.
Re: program to an interface not implementation
Quote:
Originally Posted by TheCPUWizard
In one case the calling program "knows" that your UserControl class has implemented the methods. It created the object, it knows it's type.
When programming to a pure interface, the actual object type is not even known.
CPUWizard, could you give a brief code snippet of your statement 'When programming to a pure interface, the actual object type is not even known'
Re: program to an interface not implementation
Sure (psuedo code!)
Code:
// Interface....
public interface TheInterface
{
void SomeFunction();
}
// Implementations
class A : TheInterface
{
public voide SomeFunction() { Console.WriteLine("A is running"); }
}
class B : TheInterface
{
public voide SomeFunction() { Console.WriteLine("B is running"); }
}
// "User Code"
{
void Testy(TheInterface instance)
{
instance.SomeFunction();
}
Which class is going to be passed in....???