Home  /  Questions  /  Question

60   95.3
Jan 30, 2011

Why do we need to use interfaces?


I am new to Design Patterns and just wanted to know what the main advantages are of using Interfaces.


50   50
Jun 15, 2011
In single inheritance languages like C# and Java a class can only inherit implementation from a single base class, but it can inherit features from multiple interfaces e.g. IScalable, ISerializable, etc...

This is essential what enables us to write code that can iterate through collections of objects that are NOT of same type (class inheritance), but that share the same specification (interface inheritance).

The best pattern I can think of to illustrate above point, is the Observer pattern (http://www.dofactory.com/Patterns/PatternObserver.aspx). The Subject can notify any object that is an Observer (interface implementors) without needing the Observers to share any implementation.

That is design power for you.

70   96.1
Feb 01, 2011
Interfaces are an incredibly important part of .Net. As Robert Blixt has said they provide incredible flexibility through polymorphism.

There are many examples where you are required to implement a particular interface in order to be able to take advantage of certain approaches.

Take for example the using Statement the object supplied to the using statement must implement the IDisposable interface. See http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx


Another example would be in WPF, if you have implemented the MVVM design pattern and you want to attach to a command you will need to implement the ICommand interface. See http://msdn.microsoft.com/en-us/library/ms752308.aspx


Take WCF, you basically couldn’t use it without the use of interfaces.


The long and the short of it is they are your friend and you should be completely comfortable with them.

Good luck


 1 comment
The WPF example for an interface makes the most sense to me. It seems to me though if you have different flavors of the same *type*, you'd want to use inheritance for different types that share common traits with the base type. e.g. savingsAccount : Account. This way, you maximize code re-use for common functions like CreateAccount(), DeleteAccount() etc. --- Vance McCorkle  Feb 01, 2011

1,128   99.9
Jan 31, 2011
One reason I use interfaces is because it increases the flexibility of the code. Let's say we got a method that takes an object of class type Account as parameter, such as:
public void DoSomething(Account account) {
  // Do awesome stuff here.

The problem with this, is that the method parameter is fixed towards an implementation of an account. This is fine if you would never need any other type of account. Take this example, which instead uses an account interface as parameter.
public void DoSomething(IAccount account) {
  // Do awesome stuff here.
This solution is not fixed towards an implementation, which means that I can pass it a SuperSavingsAccount or a ExclusiveAccount (both implementing the IAccount interface) and get different behavior for each implemented account.
As always though, don't over-engineer your code if YAGNI :o)

Hope that helps.