Newbie question on proper use of Interface

D

dsh0105

Hi all,

I'm writing a program as part of attempt to get a grasp on the Java
language. For the part of the program I need to implement several
rules-based sorting methods to sort an ArrayList.
From what I understand, this might be a good place to use an Interface.
I create an interface that defines all the methods each sort routine
must implement, then create concrete classes for each of the sorting
methods to be called as needed. Since they will all implement the same
interface, they should be swapable on an as-needed basis (including at
run-time?).

This would also give the added advantage of being able to apply
multiple sort rutines to the say list in any given order.

So my question is: is my logic sound? Is this this (at least one of)
the reasons a person would use an Interface?

Thanks,

David
 
D

dwalter

Yes this is one of the reaons you would use an interface, although I
can only think of only one method a sorting interface defines and that
is the sort function. and if your sort routines are correct then
sorting more than once should do nothing.
 
R

Roedy Green

So my question is: is my logic sound? Is this this (at least one of)
the reasons a person would use an Interface?

You might have a peek at some of my sorts that come with source. see
http://mindprod.com/jgloss/sort.html

or the Java built in sorts. You want to read up on Comparator and
Comparable. They are the only interfaces you need for a sort. You will
just irritate people if you reinvent a Comparator-wheel since they
already have Comparators written to use in Sun sort and Sun
Collections.


see http://mindprod.com/jgloss/comparable.html
http://mindprod.com/jgloss/comparator.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top