Simple code design question
I have a 3 layer app. The second layer is receiving and object from the 3rd layer and there are two attributes from this object that I need to pass on to the 1st layer (UI). Should I make this object's class visible in the 1st layer (by im开发者_如何学Cporting it's package/namespace, which I'm trying to avoid) or should I pass on these attributes as an string array (I'd rather have an Object array, but they're a string and an int types in Java 1.4) and cast them to their proper types in compilation time (Their types are fixed, so thankfully this is an option)?
That's what DTOs (Data Transfer Objects) are for. So in short: use Objects in your top-level interface.
I'm no java expert, but I can offer some general advice from a .NET perspective. Since that's your background it should be relevant to you.
Should I make this object's class visible in the 1st layer (by importing it's package/namespace, which I'm trying to avoid
Its OK to import the namespace. While it may feel like you decrease coupling, this is better than a string array as it does not require expensive boxing.
Do not forget that readability is always a serious consideration. I recommend you take a look at the MVC pattern.
Sounds like JavaBeans are your friend, although you'll probably not profit from reusability/serializabiliy.
JavaBeans are classic DTOs (Data Transfer Objects), used to pass along information in your program.
What kind of UI do you have? Swing? Then DTOs is probably the way to go. Consider to put the DTO classes in a project of its own, which then can be shared by the client side and the server side.
精彩评论