advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Reviews : New Tricks With Design Pattern: Java Value Types :

The equals method must also be implemented to determine equality between objects. This is an important aspect of JVT objects, because it is needed to manage the state properly. Since JVTs are busy transporting information around between sub-systems, it is important for these sub-systems to frequently check the state of the object to determine changes in this state. The user interface checks state to determine changes by the user on a web page. The data access checks equality to determine the need to persist the object. Because of this the equals method should compare all the state attributes, but avoid comparing the entity identifier info(unless there is an explicit requirement to do so).

Notice that in the ShopItem example, every product number represents an entirely different product type, so it is possible that all one has to do to see if two instances represent the same thing is to check whether those identifiers match. However, consider the example of customers. If two customers have different customer numbers, but all of their name, contact info, and credit card data matches, then they must be the same person, and the multiple instances is a mistake. Therefore you want to check all of the fields for equality rather than just the customer number.

Finally, you may have an instance where the identifiers in the JVT's are for the instances of the JVT's themselves, not the entities those JVT's represent. An example would be when you are using JVT's to take snapshots of the state of an EJB used in a lot of contexts. You may decide that the unique identifier for each JVT instance is unimportant and that you want to compare all the other fields so that you can do something like take a snapshot of an EJB before beginning a transaction, go perform some other work, then before you begin changing that actual EJB, get another instance of the JVT for it and compare your two instances to see if the EJB has changed.

Other methods can be useful in JVTs, such as the clone() method to enable a "deep" copy of the JVT, as well as methods to help properly manage "dirty" attributes. These are not necessary in JVTs, but are Highly recommended.

JVTs can also have identifier attributes, such as a name or ID. These can be used in conjunction with the ServiceLocator pattern to decouple and simplify the processing of the JVT by a JMS façade or EJB. The value of the name or ID is not so important, only that it is unique to the entity, such as an item number for a ShopItem in our previous example. By passing the itemID to the ServiceLocator, it can find the EJB, pass the ID and return the JVT without ever exposing the implementation components to the calling sub-system.

If the caller, for example, is a JMS façade, it can generate an appropriate JMS message with the JVT state. Then if the client requests a change to the EJB, it just changes the state info in the JVT and passes it back via a message to the JMS façade. The JMS façade can pass it directly on to an updateShopItemInfo(itemJVT) method which will look up the itemID from the JVT, locate the appropriate EJB, and pass the JVT to that EJB for the actual update to be performed.

This pattern extends the advantages of the value object pattern, and fosters separation of technologies, consolidation of network and component accesses, and easing the interaction with EJBs. The decoupling of clients from the EJBs is very beneficial, because it opens up many options such as enabling clients to access EJB, JMS, and web service clients from the same code. In this way the Java Value Type object works as an adapter to accommodate both an EJB producer/consumer and an XML producer/consumer.

Transaction handling can also benefit from JVT's. Consider that if you need to update five fields of an EJB in a transactional manner, you are going to have to hold a lock on that EJB for the length of time that it takes to do all five of those updates. Consider how long each of those update calls could take if the client is on a remote machine. Instead, if you pass a JVT with the updated values in to the EJB and let the EJB update its state to what is in the JVT, then it can be guaranteed that all of the updates are local and therefore faster, and you can hold the lock for the minimum amount of time possible. Furthermore, capturing the state can be made atomic efficiently. You get the same advantage where one method can be called that holds the lock for the minimum amount of time while it gathers all of the state data from that instant in time without fear of the data changing or holding the lock excessively.

There is one other less commonly used feature. When one begins a transaction that involves multiple entities, it can get JVT's of the state from each object, make the changes as required to the copies of those JVTs, and then, it can query those entities for new state JVT's to see if the state had changed since it first queried the entities before actually committing the changes back to the entities. If not, it can go ahead and commit. If the state had changed, it can decide how to handle the changes.

JVT's are useful in converting to and from XML in that they are a nice consolidation of all of the non-transient state of an entity, and therefore it is easy to write an adapter that will run through a JVT, call all of the get methods and store the data into an XML file in tags named after the properties. This is mostly just an advantage that comes from the clear separation of persistent state data into one place.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs