This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
As a general rule, use the most abstract reference object you can, especially in any externally facing element of your application (e.g., the public properties and methods, or API).
(It's even better to use Collection<T> if you can.)
This practices helps aid in encapsulation, by hiding implementation details.
For example, perhaps you initially think HashSet<T> is the right implemenation to use. You start your code, then realize what you really need is TreeSet<T>. Or even to write your own Set<T> implementation.
If all your external references are to HashSet<T>, you have to update larger parts of your program.
If your public accessors are all Set<T>, however, you only have to modify the affected class, thus minimizing the ripple effect of the change.
Which is a significant benefit of well-thought out Object Oriented Design.
If you find in your code that you feel forced to declare public methods or properties with specific classes instead of interfaces (when applicable), it's often a sign you need to reexamine the design.
[ April 25, 2008: Message edited by: Stevi Deter ] [ April 25, 2008: Message edited by: Stevi Deter ]
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Ivan Ivanic
Ranch Hand
Joined: Oct 31, 2007
Posts: 100
posted
0
i would add: if you program to interfaces you are achieving loose coupling. In example: if you have:
Hope this helps you understand that you can choose how you are gonna program - to interfaces of concrete implementations - but that you should always program to interfaces.
I would caution against the prescription to "always" program to interfaces.
Rather, use when it fits the need.
For example, on the large N-Tier projects I've worked on, it has always been of benefit to program the service layer and DAO to interfaces. This lets us use tools like Spring to isolate various elements, swap in and out implementations between JDBC and Hibernate, etc., with ease.
But I've never found it useful to program the actual model or the presentation layer to interfaces. Any attempt usually just results in the sort of difficult to maintain and understand code that makes some people criticize Java.
In Java, the "always" is always think about what patterns, methods, and approaches to object oriented design are going to help you write clean, readable, maintainable code.
Ivan Ivanic
Ranch Hand
Joined: Oct 31, 2007
Posts: 100
posted
0
thanks Stevi, I will remember this. Still learning
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
posted
0
What i see in your code is public used many times. A source file can have only one public class. Morever the interface can't be public I think it is defaultly public. Correct mei am wrong .
class Eater { public void eat(Eatable e) { //eat Eatable object no matter what is objects type //this Eater only cares that it is Eatable and that it can call // it's eatMe() method... e.eatMe(); //here Eater don't need to know anything else about e } } interface Eatable { public void eatMe();// all Eatable objects must have this behavior } class Apple implements Eatable { public void eatMe() { System.out.println("Apple eated"); } // a lot of other code } class Paper implements Eatable { public void eatMe() { System.out.println("Paper eated"); } // a lot of other code } public class TestEating { public static void main(String[]a) { Eater eater = new Eater(); Apple apple = new Apple(); Paper paper = new Paper(); eater.eat(apple);// here eater doesn't know anything else eater.eat(paper);// about this two objects, except that they are // eatable, thus you have a very loose coupling }} } }
Morever the interface can't be public I think it is defaultly public. Correct mei am wrong .
Actually, it's the methods of an interface that are implicitly declared public. For full details about what access modifiers are valid for interfaces and when, see the Java Language Specification. [ April 28, 2008: Message edited by: Stevi Deter ]
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
posted
0
What about memeber variables.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
Originally posted by Dinesh Tahiliani: What about memeber variables.
Interfaces can declare fields, but they are implicitly public, static, and final. [ April 28, 2008: Message edited by: Stevi Deter ]
Ivan Ivanic
Ranch Hand
Joined: Oct 31, 2007
Posts: 100
posted
0
Originally posted by Dinesh Tahiliani: What i see in your code is public used many times. A source file can have only one public class. Morever the interface can't be public I think it is defaultly public. Correct mei am wrong .