Example Source Code For Value Objects

I am starting on a new project that is using the J2EE platform. This application is a database maintenance application mainly doing updates to database tables. I have a good understanding of the main concepts/patterns I want to incorporate but don't know how to actually go about implementing them. For instance, I like the idea of a FrontController managing all requests and then delegating the request to the appropriate handler(s). I like the idea of value objects and data access objects probably using session beans for the validating of data. I just don't know how all the pieces fit together. I'd like to see an example of a jsp page issuing a request that retrieves data and displays it on the page using the concepts mentioned above. For instance, where does the value object come into play? Is it accessed from the JSP page, or does it get returned from the Front Controller being issued as a request. I have the pet store demo, but I don't see any examples of what I am trying to accomplish. Anyone know of some?

[1041 byte] By [dudleyda] at [2007-9-26 21:12:50]
# 1

Code is pretty lengthy, but here are the generalities of what I did recently along those lines:

(1) created an Entity bean (cmp) to access a record from the database table.

(2) created a ValueObject (just a simple Java plain object with 4 instance variables. Object implements the java.io.Serializable interface so it can be transmitted across the network)

(3) added a method on the Entity bean called "getData()". In this method, the Entity bean instantiates the ValueObject and sets the ValueObject's instance vars to the Entity Bean's vars. It then returns the plain ValueObject to the requestor.

(4) created a stateless session bean to simply take care of all the specifics of interfacing with the Entity Bean (so the client JSP page doesn't have to know about which Entity Bean is being called, etc.

(5) created the JSP page that calls the Stateless Session bean. Stateless SB has a method on it called "getInfoForRecID(BigDecimal recID)". JSP gets home interface and remote interface to the Session Bean and then sets recID to a valid DB table rec ID.

(6) Stateless Session Bean's getInfoForRecID method gets home interface to Entity Bean, then uses the "findByPrimaryKey(recID)" method to get a handle to the Entity Bean's remote interface. Finally, the session bean calls the Entity Bean's "getData()" method and gets back the ValueObject. Session bean passes this object back to the JSP. The JSP then extracts the values from the ValueObject and displays them.

Just start simple and follow it through one example and you'll see how the pieces work together. If you want, you can also implement the BusinessDelegate pattern by NOT having the JSP directly call the Session bean. Instead, the JSP can call a helper object (another plain Java class that acts as a business delegate to handle the general requests from the JSP). The Business Delegate offers the advantage of buffering the JSP from having to know anything about the App Server, the context objects, etc. Furthermore, a LOT of JSP pages can all use the same business delegate, so you're simplifying your code by not having to put connectivity/RMI logic in all your JSP pages.

davewiltz at 2007-7-3 20:40:46 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
The only thing I would add to the above example is that I have my Entity Bean inherit from the Value Object. Other than that it is the same as above.
jebrick at 2007-7-3 20:40:46 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> (3) added a method on the Entity bean called

> "getData()". In this method, the Entity bean

> instantiates the ValueObject and sets the

> ValueObject's instance vars to the Entity Bean's vars.

> It then returns the plain ValueObject to the

> requestor.

IMHO a preferable perspective from an OO theory view is to think in terms of creating and returning the VO and not getting the 'data', even though the VO is 'Data'. This is really just a issue of the approach to abstraction.

> (5) created the JSP page that calls the Stateless

> Session bean. Stateless SB has a method on it called

> "getInfoForRecID(BigDecimal recID)". JSP gets home

> interface and remote interface to the Session Bean and

> then sets recID to a valid DB table rec ID.

An alternative, is to take the approach of the View Handling element of you front controller accessing the Session Bean and handling the forwarding to the JSP, then have the VO[s] added to the request.

> The JSP then

> extracts the values from the ValueObject and displays

> them.

I prefer to ask to VO[s] to express themselves rather than ask them for their values. If the VO is a [plain] old Java Bean, a nice addition is to extend this VO into a Custom tag[s], that can express it's self in multiple ways. i.e. VO.toString(), VO.toXml() etc rather the VO.GetData().

This allows the VO's to be handled polymorphically, and so you can create very neat simple Custom tags. i.e.

<ctl:address format='block'/>

<ctl:address format='line'/>

<ctl:address format='form'/>

MartinS. at 2007-7-3 20:40:46 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

> I prefer to ask to VO[s] to express themselves rather than ask them for their values.

> If the VO is a [plain] old Java Bean, a nice addition is to extend this VO into a Custom tag[s],

> that can express it's self in multiple ways. i.e. VO.toString(), VO.toXml() etc rather the VO.GetData().

> This allows the VO's to be handled polymorphically, and so you can create very neat simple Custom tags.

> i.e.

> <ctl:address format='block'/>

> <ctl:address format='line'/>

> <ctl:address format='form'/>

I like that concept. Is it possible to get some example code?

I would do anything to get it (almost anything :-))

If there's to much code then by e-mail would be fine. tma@tmaonline.dk

Please!

Thanks in advance.

ThomasMA10 at 2007-7-3 20:40:46 > top of Java-index,Other Topics,Patterns & OO Design...