File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Any good Design Patterns for extending Value Objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Any good Design Patterns for extending Value Objects?" Watch "Any good Design Patterns for extending Value Objects?" New topic
Author

Any good Design Patterns for extending Value Objects?

dave taubler
Ranch Hand

Joined: May 15, 2001
Posts: 132
Hi,

I am using one estbalished Design Pattern, the Value Object pattern, to pass data from my EJB tier to my Web tier. The issue I seem to constantly come across is that depending on the situation, I somtimes want a little extra information sent along with a partiuclar Value Object. A simplified example:

Say I have a User object. Pretty much anytme I pass a User object from the EJBs to my Web app, I want that user's:
- ID
- First Name
- Last Name
- Email address
In some situations, however, I want to pass along a special "Display Name" value that is constructed by my EJBs. In other, rare, situations I want information about the relationship between this User and another User. Maybe in another very specific circumstance, I want information about how long the user has been online.

So I could of course add these fields to my User object, and wind up with a huge Value Object that passes along a bunch of fields that are, in most cases irrelevant. But that would get messy, cumbersome to maintain, and somewhat ineffecient in terms of network data transfer.

So I am looking to see if there are any design patterns that exist for such a dilemna. For example, the Decorator pattern is sort of the approach I am looking for, except that it's designed more to alter the behavior of the object, not the interface (i.e. I couldn't use it to add a getDisplayName() method to the User object.) I've been looking through pattern catalogs, but haven't yet found anything useful.

Any thoughts?


Dave Taubler<br />Specializing in <a href="http://taubler.com/articles/" target="_blank" rel="nofollow">Java and Web Development</a>
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Maybe another solution would imply the usage of a DynaBeans.

--
:alex |.::the_mindstorm::.


blog - InfoQ.com
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
How does the client side know which methods the objects will have? Does it already know from context, or will it need to probe the VO?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
dave taubler
Ranch Hand

Joined: May 15, 2001
Posts: 132
DynaBeans... we're not using Struts (I'm not sure if DynaBeans would work here even if we were) but maybe I will check out the general structure to see if it would work.

Ideally, I would want the client to know the VO's interface and not have to probe. Especially with the other programmers I'm working with , I'm really into type-safety these days.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by dave taubler:
DynaBeans... we're not using Struts (I'm not sure if DynaBeans would work here even if we were) but maybe I will check out the general structure to see if it would work.

Ideally, I would want the client to know the VO's interface and not have to probe. Especially with the other programmers I'm working with , I'm really into type-safety these days.


DynaBeans are not Struts-specific. They are supported in Commons BeanUtils, too.


James Carman, President<br />Carman Consulting, Inc.
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
You should use composition. Create a new value object type that includes a copy of a UserVO along with the additional information.

Example:



Note both UserVO and DisplayUserVO are immutable and cannot be extended. This pattern is preferable to subclassing in most situations.

Best regards
Geoffrey
[ March 14, 2005: Message edited by: Geoffrey Falk ]

Sun Certified Programmer for the Java 2 Platform
 
jQuery in Action, 2nd edition
 
subject: Any good Design Patterns for extending Value Objects?