This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes What do you mean by 'code to an interface'? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "What do you mean by Watch "What do you mean by New topic
Author

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

Amit Saini
Ranch Hand

Joined: Oct 20, 2004
Posts: 280
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

Joined: Oct 25, 2000
Posts: 7292

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.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Thomas Taeger
Ranch Hand

Joined: Dec 16, 2002
Posts: 307
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


www.classic-and-class.com - www.evalulearn.com
Interfaces are the glue of OO.
Amit Saini
Ranch Hand

Joined: Oct 20, 2004
Posts: 280
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The Interface Segregation Principle might also be of interest to you: http://www.objectmentor.com/resources/articles/isp.pdf


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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

Joined: Oct 20, 2004
Posts: 280
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Amit Saini
Ranch Hand

Joined: Oct 20, 2004
Posts: 280
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

Joined: Jan 29, 2003
Posts: 8791
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What do you mean by 'code to an interface'?
 
Similar Threads
Question on Interface
do they exist abstract interfaces?
EJBObject question.
Doubt in servlet API
can Interface Implement and extend?