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??
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.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009 In theory, there is no difference between theory and paractice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
// 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....???
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009 In theory, there is no difference between theory and paractice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Bookmarks