• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What do you mean by 'code to an interface'?

 
Amit Saini
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep reading this line in a lot of books. I'm just not too clear about it.
Can anyone throw some light on what is meant by 'code to an interface' as a best practice?
Thank you,
Amit
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, first define/name all the methods that are supported by some type. Once you know exactly what the class can do, then write the code that supports these actions.

The idea of writing to an interface means you are declaring your class' behavior first. The implementation for it only comes after you know what services the class offers.
 
Thomas Taeger
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have designed a pritty large system with many Sequence Diagrams, and we were able to use interfaces in aproximately 95% of the cases, and just in 5% of the cases we needed to use (mostly abstract) classess within the sequences.

We designed with what interfaces promise how some class/object will behave later.

The methods of an interfaces are fully sufficient to describe their behaviour.

Using interfaces as parameters you can pass around nearly any object as long as its class implements this one interface too.

Finally using interfaces makes it easier to live in a multi-inheriting world, but that is not the important reason.

You can design 95% of a system by declaring interfaces and without caring about any implementation.

Thomas
 
Amit Saini
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your responses.
Can you guys point me to some resource that gives me an actual example of 'coding to interfaces' in action?
I guess I'd like to see some code and try it out myself on the compiler to see how it works. That will help me to understand this concept better.
Thanks again,
Amit
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boiled down to the barest bones it means use interface List

instead of concrete class LinkedList

If I write my method to depend on the List interface you can pass me a LinkedList, an ArrayList or YourOwnCrazyListImplementation. That improves the chances of my method being useful in new situations or surviving changes in your part of the program.

In more advanced cases we can use interfaces to manage dependencies between one package or component and another. I wrote a little blurb on Dependency Inversion that may make it clearer - or more confusing. Let me know which.
[ September 09, 2005: Message edited by: Stan James ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Interface Segregation Principle might also be of interest to you: http://www.objectmentor.com/resources/articles/isp.pdf
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll go one better. This link is an article by Erich Gamma on an artifical but instructive walkthrough on designing an application using patterns. Such an approach requires that you write interfaces first.

It's not supereasy to follow right off, but is well worth careful study.
 
Amit Saini
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,
So the whole point of coding to interfaces is flexibility, is that correct? Is flexibility the only benefit or are there more?

By using a superclass, you can have its reference variable point to any of its children? Is my understanding correct?

(I'm new to designs and hence take a little time to understand it)

I will also peruse the links given by the others.
Thanks again for your responses,
Amit

PS: Do people generally take time to understand design patterns /design related stuff or am I the only one finding this time consuming
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Saini:

PS: Do people generally take time to understand design patterns /design related stuff or am I the only one finding this time consuming


Don't worry. It takes time and experience to understand design patterns or, more generally, good design. Ilja was just reminding me of this the other day: you don't really appreciate a good solution until you've experienced the problem.

In fact, the whole idea of teaching "design patterns" to people who don't have a lot of design experience is a little worrisome. What sometimes happens is that, given abstract knowledge of patterns and no real design experience, the fledgling designer assumes that have to use every pattern in every program, or have to use a named pattern for everything they do. Part of understanding design patterns well is understanding when and how to apply them appropriately.
 
Amit Saini
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest.
That was a nice post. I feel you're right when you say that one cannot appreciate a good design until you have experienced the problem.

the fledgling designer assumes that have to use every pattern in every program, or have to use a named pattern for everything they do


I'm a recent graduate so me and a lot of my friends thought this way in university). "Hey, this pattern is so Cool, lets put it in." ..without actually knowing if its going to help.

I'm going to start off reading some Head First Design Patterns to gain some understanding of what design patterns do and try out some code samples.

[ September 10, 2005: Message edited by: Amit Saini ]
[ September 10, 2005: Message edited by: Amit Saini ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,
So the whole point of coding to interfaces is flexibility, is that correct? Is flexibility the only benefit or are there more?

By using a superclass, you can have its reference variable point to any of its children? Is my understanding correct?


Flexibility is a key concept, but there are a lot of ways to look at it. What's the goal of flexibility? Probably foremost to reduce the cost of maintaining and enhancing the system over time. We try to "close" certain stable parts of the system to modification so we don't have to touch them (zero cost after they are built) and isolate things that might change so they don't break other things when we change them. A second goal might be reuse. If we decouple part of a system well enough someone else can use it in another system without dragging along a lot of other baggage. Many of the patterns and OO principles (like Robert Martin's 11) revolve around getting dependencies right so these good things can happen.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic