Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Programming to an intreface?

 
Joe Harry
Ranch Hand
Posts: 10091
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Can anyone please explain me with a scenario about what it means by saying programming to an interface rather than implementation?

Thanks in advance!
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example from using the Collections framework would be that instead of a line like

ArrayList myList = new ArrayList()

you would use

List myList = new ArrayList()

This has the benefit that if you later decide that ArrayList should be changed to some other class that implements List, you only need to change a single line of code. This is especially important in public APIs. You should not have an API method like

public ArrayList myMethod (HashMap myMap) { ... }

but instead

public List myMethod (Map myMap) { ... }
 
Joe Harry
Ranch Hand
Posts: 10091
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

So that means we always have to use the supertype reference and dynamically decide on the object. Am I right???
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certainly not always. If you find that the difference between, say, List and ArrayList matters to your code -because you're using ArrayList methods that are not part of List- then that's an indicator for using it as ArrayList, and not List. Otherwise, plenty of instanceof calls and type casts pile up quickly. If all uses of a particular type are within the same class (and certainly if they are within the same method), then using the more specific type probably makes sense. But there are no hard rules, so it's always a bit of a judgement call that depends on the circumstances.
 
Mike Isano
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it means using polymorphism.



I don't like the ArrayList example.

If we have something like this:

public class Monster
{
Person[] personArray;

public void killEveryone(Weapon w)
{
//for each person in the array
for(Person p : this.PersonArray)
{
w.attack(p);
}
}

}//end of class


The weapon in the killEveryone method is not a specific weapon like "sword" or "gun". It's just a theoretical weapon. No matter what kind of specific weapon you pass in, the killEveryone method will work.

Weapon could be an interface or a class.

The advantage of this is that the code of actually killing someone could be very different. If you attack someone with a gun you need to display special graphics of bullets flying, etc. When you attack someone with a sword the distance between the monster and the person needs to be calculated to make sure it's in melee range, etc. These differences no longer matter becuase your only interested in the high level concept that you are attacking people with a weapon. Changes in the specific weapon classes don't effect the high level algorithm.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Certainly not always. If you find that the difference between, say, List and ArrayList matters to your code -because you're using ArrayList methods that are not part of List- then that's an indicator for using it as ArrayList, and not List. Otherwise, plenty of instanceof calls and type casts pile up quickly. If all uses of a particular type are within the same class (and certainly if they are within the same method), then using the more specific type probably makes sense. But there are no hard rules, so it's always a bit of a judgement call that depends on the circumstances.


I use that rule too. ArrayList within a class, but once it leaves the class its List all the way.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic