Re: Create a JAVA Client/Server app in 5 Minutes
Andreas Otto wrote:
that's what i [sic] don't understand too because i [sic] had a working solution with
ArrayList fine but why i [sic] should change it to List ?
It's not really a question of changing it to 'List' but of what one
should do in the first place.
It is a fundamental principle of O-O programming, and good programming
generally, to "program to the interface, not the implementation".
GIYF:
<http://www.google.com/search?q=program+to+the+interface>
In summary, declaring a variable as the widest appropriate type gives
the greatest flexibility to the API or the program.
For example, if you wanted your outer 'List' to be a
'CopyOnWriteArrayList' or a 'Collections.synchronizedList(new
ArrayList<?>())', you'd only have to change one line, the declaration,
to get your desired behaviors.
When you declare the variable to be a specific concrete type, and more
importantly when you declare the public methods to return or accept
that type, you have committed your code to that specific
implementation in perpetuity. If that concrete type embodies
behaviors not essential to your algorithm, then you specified it too
narrowly.
"Program to the interface", or as I prefer, "Program to the type", is
one of those essential principles of software development of which all
practitioners should be aware.
It is especially significant for frameworks intended for use by a wide
audience to incorporate in their own works. Such projects need to
adhere to the highest possible standards.
--
Lew