Back to list
Views:   0
Replies:  0
Archived
,
Reply 1
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.
Keld Ølykke, Jun 15, 2011
Reply 2
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

Ricky

Ricky Owen, Feb 01, 2011
Reply 3
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.

Robert Blixt, Jan 31, 2011
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas