| | |
why use interfaces?
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
Wow, I was just explaining interfaces to a coworker the other day. Freaky.
>Does any one know a robust benefit of using interfaces?
Consider the concept of data hiding. Let's say you want to provide a class to some client code, but don't want to give them full access. One way to restrict access without limiting the functionality of your class is to implement an interface and require the client code to get objects through a factory:
This is especially useful when interfacing with COM.
Another benefit of interfaces is team development. If you have a dozen programmers working on different pieces of code that need to connect, it's best to define interfaces for the connections. As long as the programmers write according to the interfaces, everything will link together nicely regardless of personal choices and style.
Yet another benefit is that with interfaces you can use a class without having defined it first. For example, if you have a class that does a lot of intricate work and won't be finished before the rest of the project, the rest of the project can use an interface to it and avoid being stalled by the development of that one class.
Personally, I like the guarantees that interfaces provide. If a class implements IDisposable, I know not only that the objects can be disposed without getting compiler errors, but that they should be disposed. This keeps my code consistent and robust. You can provide similar guarantees in your code simply by adhering to an interface.
>Does any one know a robust benefit of using interfaces?
Consider the concept of data hiding. Let's say you want to provide a class to some client code, but don't want to give them full access. One way to restrict access without limiting the functionality of your class is to implement an interface and require the client code to get objects through a factory:
C# Syntax (Toggle Plain Text)
using System; namespace JSW { public class Program { static void Main() { IFoo myfoo = FooFactory.GetFoo(); Bar mybar = new Bar(); myfoo.Print(); mybar.Print(); mybar.Test(); } } // Restricted type for clients public interface IFoo { void Print(); } // Factory for clients public class FooFactory { public static IFoo GetFoo() { return new Bar(); } } // Fully functional type for us internal class Bar: IFoo { public void Print() { Console.WriteLine( "Foo!" ); } public void Test() { Console.WriteLine( "Test" ); } } }
Another benefit of interfaces is team development. If you have a dozen programmers working on different pieces of code that need to connect, it's best to define interfaces for the connections. As long as the programmers write according to the interfaces, everything will link together nicely regardless of personal choices and style.
Yet another benefit is that with interfaces you can use a class without having defined it first. For example, if you have a class that does a lot of intricate work and won't be finished before the rest of the project, the rest of the project can use an interface to it and avoid being stalled by the development of that one class.
Personally, I like the guarantees that interfaces provide. If a class implements IDisposable, I know not only that the objects can be disposed without getting compiler errors, but that they should be disposed. This keeps my code consistent and robust. You can provide similar guarantees in your code simply by adhering to an interface.
Last edited by Narue; Mar 17th, 2008 at 12:34 pm.
I'm here to prove you wrong.
A great article which makes you understand when to use abstract classes and when you use interfaces
Interfaces: http://fci-h.blogspot.com/2008/03/oo...rfaces_05.html
Abstract classes: http://fci-h.blogspot.com/2008/03/oo...t-classes.html
Interfaces: http://fci-h.blogspot.com/2008/03/oo...rfaces_05.html
Abstract classes: http://fci-h.blogspot.com/2008/03/oo...t-classes.html
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Oct 2007
Posts: 172
Reputation:
Solved Threads: 16
I think that in addition to what has said in http://fci-h.blogspot.com/2008/03/oo...rfaces_05.html about interfaces witch i find very interesting in fact.
I can add other think
Interfaces are comming to the world to avoid the multiples inheritences concept adopted for example by C++ language that causes somes problems and confusions when using this concept and that brings some troubles to the the OOP paradigm it self
I give you a concrete example:
You know this kind of jet that can either fly and navigates throw the sea like the H-4 Hercule jet modelhttp://tuxthepenguin.free.fr/img/Lun.jpg
This kind of object is a jet however it has an additional functionality that can be found in the boat object to avoid the double inheritance from boat and jet objects to define the H-4 we do as so:
class jet
{
....
fly()
{// some code here}
....
}
class boat, INavigate
{
.....
navigate()
{// some code here}
......
}
interface INavigate
{
navigate();
}
class H4 : Jet,INavigate
{
.....
new fly()
{// some code here}
......
navigate()
{// Some implementations here}
}
I can add other think
Interfaces are comming to the world to avoid the multiples inheritences concept adopted for example by C++ language that causes somes problems and confusions when using this concept and that brings some troubles to the the OOP paradigm it self
I give you a concrete example:
You know this kind of jet that can either fly and navigates throw the sea like the H-4 Hercule jet modelhttp://tuxthepenguin.free.fr/img/Lun.jpg
This kind of object is a jet however it has an additional functionality that can be found in the boat object to avoid the double inheritance from boat and jet objects to define the H-4 we do as so:
class jet
{
....
fly()
{// some code here}
....
}
class boat, INavigate
{
.....
navigate()
{// some code here}
......
}
interface INavigate
{
navigate();
}
class H4 : Jet,INavigate
{
.....
new fly()
{// some code here}
......
navigate()
{// Some implementations here}
}
•
•
Join Date: Jan 2008
Posts: 2,052
Reputation:
Solved Threads: 118
•
•
•
•
Wow, I was just explaining interfaces to a coworker the other day. Freaky.
>Does any one know a robust benefit of using interfaces?
Consider the concept of data hiding. Let's say you want to provide a class to some client code, but don't want to give them full access. One way to restrict access without limiting the functionality of your class is to implement an interface and require the client code to get objects through a factory:
This is especially useful when interfacing with COM.C# Syntax (Toggle Plain Text)
using System; namespace JSW { public class Program { static void Main() { IFoo myfoo = FooFactory.GetFoo(); Bar mybar = new Bar(); myfoo.Print(); mybar.Print(); mybar.Test(); } } // Restricted type for clients public interface IFoo { void Print(); } // Factory for clients public class FooFactory { public static IFoo GetFoo() { return new Bar(); } } // Fully functional type for us internal class Bar: IFoo { public void Print() { Console.WriteLine( "Foo!" ); } public void Test() { Console.WriteLine( "Test" ); } } }
Another benefit of interfaces is team development. If you have a dozen programmers working on different pieces of code that need to connect, it's best to define interfaces for the connections. As long as the programmers write according to the interfaces, everything will link together nicely regardless of personal choices and style.
Yet another benefit is that with interfaces you can use a class without having defined it first. For example, if you have a class that does a lot of intricate work and won't be finished before the rest of the project, the rest of the project can use an interface to it and avoid being stalled by the development of that one class.
Personally, I like the guarantees that interfaces provide. If a class implements IDisposable, I know not only that the objects can be disposed without getting compiler errors, but that they should be disposed. This keeps my code consistent and robust. You can provide similar guarantees in your code simply by adhering to an interface.
Due to lack of freedom of speech, i no longer post on this website.
![]() |
Similar Threads
- interfaces in java (Java)
- interfaces and extending (Java)
- Interfaces (Java)
- message passing interfaces (C)
Other Threads in the C# Forum
- Previous Thread: Binary File IO
- Next Thread: Command Line Arguments
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox concurrency control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ globalization httpwebrequest image index input install java label list listbox listener mandelbrot math microsoftc#visualexpress mouseclick mysql networking operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






