views:

473

answers:

7

Hey guys,

I often hear/read about interfaced based programming but I am not exactly clear on what that really means. Is interfaced based programming an actual stand alone topic that actually has books written about it? If so, can anyone recommend any good ones?

I came across interface based programming as I was reading about how good APIs are designed and would like to learn more about it. Right now I am not clear how to properly go about designing an API around interfaces.

Any info is greatly appreciated.

Thanks.

+9  A: 

It's basically a matter of expressing your dependencies in terms of interfaces instead of concrete classes (or worse, static methods). So if one of your classes needs to perform authentication, it should be provided an IAuthenticator (or whatever).

This means that:

  • You can write your code before implementing the real dependency
  • You can test via mocking really easily (without having to mock classes, which gets ugly)
  • It's clear what you depend on in terms of the API instead of implementation (i.e. you have looser coupling)
Jon Skeet
+5  A: 

Chapter 6 of "Practical API Design" by Jaroslav Tulach is titled "Code Against Interfaces, Not Implementations". It explains that, by coding against an interface rather than a specific implementation, you can decouple modules (or components) in a system and therefore raise the system quality.

Bertrand Meyer in OOSC2 explains clearly why "closing" a system and making it more modular raises its quality.

CesarGon
+2  A: 

If you google for interfaces, you will find plenty of information about how useful interfaces may be.

The concept is to define clear interfaces, which will be used by the various components/parts/classes/modules to communicate and interact. Once you have defined what should be the input/output of these interfaces, you can let the various teams to develop whatever is needed to fulfill the interface requirements, including the input/output testing, ... etc

If you follow this pattern, various teams can start developing their part without waiting for the other parts to be ready. In addition to that you can use Unit testing (using fake objects to simulate the other parts which you are not developing, and test your part).

This method is quite the standard for any new programming project and everybody will take this for granted.

BlueTrin
A: 

This is something that I don't recommend using heavily in C# development.

Interface-based programming is basically programming to interfaces. You develop the interfaces you're going to use an Contracts, and the actual implementation of the interfaces is hidden behind these contracts.

It was very common prior to .NET, as the best way to get reusable components in Windows was via COM, which worked via interfaces everywhere. However, given .NET's ability to support multiple languages with a single runtime (the CLR), as well as it's superior support for versioning when compared with native code, the usefulness of interface based programming is lessened dramatically when you're programming in C# (unless you're trying to make COM components, in which case, you'll still be creating the COM interfaces indirectly from your C# classes).

Reed Copsey
To the downvoters: I'm not suggesting that interfaces, in and of themselves, are bad - rather, that the usefulness of designing your entire API around interfaces, which is often what "interface based programming" is referring to, is not necessarily as valid for C# developers as it was in the COM days.
Reed Copsey
Mocking is so much easier against interfaces than concrete classes (particularly sealed ones with no virtual methods). Also I still believe it clarifies what you really depend on...
Jon Skeet
@Jon SKeet: True. Mocking can be easier against interfaces than sealed or non-virtual classes, but abstract classes can be mocked just as easily, and have some advantages when versioning, if you're using C# (which was tagged in the question).
Reed Copsey
I would really appreciate it if people would explain exactly what they hate about this answer...
Reed Copsey
Well, I didn't downvote you, Reed. But I'm not exactly grokking the relevance of supporting multiple languages, and versioning support, to the discussion. I get the part about how this may be a carry-over from the good old days of COM, though.
DOK
@DOK: A large part of why interface based programming was required in the first place was to support multiple languages/technologies making reusable components. .NET provides a lot of the same support out of the box, so much of the need for using IEverything in the COM world, and other interface-based programming methodologies, is handled by the .NET framework automatically without interfaces.
Reed Copsey
A: 

What you refer to as "interface based programming" is more commonly referred to as programming to an interface. Here is an example below. The benefit is hiding the actual implementation of the interface and allowing your code to be more flexible and easily maintained in the future.

YourInterface foo = CreateYourInterface();
foo.DoWork();
foo.DoMoreWork();

CreateYourInterface() would return a concrete implementation of YourInterface. This allows you to change the functionality of the application by changing one line:

YourInterface foo = CreateYourInterface();
Taylor Leese
You changed a line of code there? Which one? Perhaps you could clarify?
DOK
YourInterface foo = CreateYourInterface() is the line I was referring to. I was just pointing out that if you assigned a different object to foo that also implemented the interface you would get different functionality with the same code.
Taylor Leese
A: 

Interface based programming can be thought of as decoupling the implementation of functionality and how you access the functionality.

You define an interface
interface ICallSomeone { public bool DialNumber(string number); }

and you write your implementation

public class callsomeone: ICallSomeone {
public bool DialNumber(string number) {
//dial number, return results }}

You use the interface without caring about the implementation. If you change the implementation, nothing cares because it just uses the interface.

Cub