|
-
May 3rd, 2007, 02:25 PM
#1
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??
-
May 3rd, 2007, 02:38 PM
#2
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.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; 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
-
May 3rd, 2007, 02:44 PM
#3
Re: program to an interface not implementation
 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'
-
May 3rd, 2007, 02:54 PM
#4
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....???
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|