Possible Duplicates:
What does it mean to program to a interface? Interface List - java
Hey there. I've been asked to use an the interface list in a couple of classes. My teacher says that this is to make it easier in the future to exchange an implementation. I've been asked to replace ArrayList by List wherever possible but am still not entirely sure what this means. I think I understand what an interface is but am not sure how to go ahead with the changes.
So for instance, this is the first part of one of the classes that I am supposed to modify all of the ArrayList bits with List. Any help is appreciated
public class Simulator
{
// Constants representing configuration information for the simulation.
// The default width for the grid.
private static final int DEFAULT_WIDTH = 70;
// The default depth of the grid.
private static final int DEFAULT_DEPTH = 40;
// List of animals in the field.
private ArrayList<Animal> animals;
I suspect you've been asked to replace any declarations of ArrayList
variables, parameters etc with the corresponding List
type... but not the instantiations. So for example, if you had
ArrayList<String> names = new ArrayList<String>();
you might replace that with
List<String> names = new ArrayList<String>();
The constructor call still refers to ArrayList
because you can't directly instantiate an interface - but the variable is of the interface type to state "I'm only using things in this interface; I don't need any ArrayList-specific members."
It simply means that you should change
private ArrayList<Animal> animals;
to
private List<Animal> animals;
and
public void myMethod(ArrayList<Animal> someList) { ... }
to
public void myMethod(List<Animal> someList) { ... }
etc.
The rationale is the following: If you require a List
, ask for a List
(and not an ArrayList
). Then your methods are more general, since they will work for ArrayList
, LinkedList
or whatever.
If you have List<Animal>
everywhere where possible, you can change from, say, ArrayList<Animal>
to LinkedList<Animal>
just by replacing the actual type being instantiated with new
.
rob,
Interfaces provide an abstraction layer that you can use to switch implementations on the future. Imagine this:
You can write a security/authentication system using the Kerberos interface but you can do the same using directly the Active Directory API from Microsoft. It's easier to use the API (of course, if you already know it). But this will prevent you from changing from AD to another solution that provides the Kerberos interface.
So far so good?
That's why your teacher told you to use interface. List is an interface for any generic list of items. It can be implemented in many many ways including one of your own (that he/she might ask as another exercise).
Inside your code all you need is to change
private ArrayList<Animal> animals;
to
private List<Animal> animals;
Since ArrayList is an implementation of the List interface, if you use the ArrayList you get too coupled with this implementation and in a near future you might need to change to another implementation (let's say for instance a read-only implementation for security or a mix implementation using an internal hashmap for performance or an implementation that instead of using only memory can swap from memory to disk and vice-versa for some reason).
you should changed it to private List<Animal> animals.
However since List is an interface(therefore does not have any constructor), you cant having animals = new List<Animal>();
Instead you should still use private List<Animal> animals = new ArrayList<Animal>();
精彩评论