[ajug-members] design theory

Laura Woody lkwoody at gmail.com
Sun May 4 23:06:58 EDT 2008


Mcdaniel,
Not as basic'ish a question as you think, considering how many developers
claiming years of java experience seem to miss the concept in practice.

At it's most basic, design to interface means to use the most generic
reference you can to an object.  My favorite practical example is the
java.util package.  If, for example, you create a method to return a group
of objects,  you should declare the return type as the most generic one
possible - java.util.Collection if your clients do not need to know anything
about the relationship of the objects in the Collection.  java.util.Set if
the items are a unique set and order doesn't matter,  java.util.List if
order is important, or java.util.Map if it is a collection of key-pairs.
Coding to the interface gives your code, and any extensions to your code the
most flexibility in implementation.  If you return the implementation.

Of course the  next step is to always be looking for the generic interfaces
in your code, and extracting those interfaces where possible.  Again to give
yourself the most flexibility in future implementations.

I agree that too often the examples are esoteric.  I think I learned the
most by studying the "Gang of Four" design patterns book, where they are
solving real world code problems.  I also had luck surfing online for good
discussions of this topic.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.ajug.org/pipermail/ajug-members/attachments/20080504/7e6ffe51/attachment.html 


More information about the ajug-members mailing list