aspose file tools*
The moose likes Java in General and the fly likes How to clone 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 "How to clone Value Objects" Watch "How to clone Value Objects" New topic
Author

How to clone Value Objects

Pete Neu
Ranch Hand

Joined: Feb 18, 2005
Posts: 86
In my application I use value objects to represent
data from a relational Database.

The problem is that I need the data in two ways. Fristly
I present the data on a presentation tier for which I need
to modify the data to make it look good. Secondly
I need the original data in an unmodified state to perfom
business Methods.

The code looks roughly like this :


DataManager dManager = new DataManager();

DatabaseVO dVO = (Database) dManager.readAllData();

this.doSomeFormating(dVO);

this.doSomeInserting(dVO); //Problem arises here orignal data is changed
//and no longer valid

What can I do to make an extra copy for the business method?
My workaround was just to instantiate the DataManager twice
and read the data a second time from the Database.
William A White
Ranch Hand

Joined: Jul 01, 2004
Posts: 35
As a general rule, I wouldn't modify the data for presentation. For instance, if I have a phone number stored in a database, on my presentation I would run the raw database value through a simple function to add dashes or whatever - on a JSP it would look something like this:

Phone Number: <%=formatPhoneNumber(databaseRecord.getPhoneNumber())%>

Modifying the actual data just leads to all kinds of trouble.
Pete Neu
Ranch Hand

Joined: Feb 18, 2005
Posts: 86
I usually stick to this method as well. But in this case a lot
of meta-data needs to be displayed and the filter logic is so complex
that I really need to modify the data otherwhise it would lead to
really confusign methods. For instance I got some type of metadata
where I can only display part of the same homogeneous data.

Is there really no way to simply clone the DatabaseVO in a way that we operate
on an idependent copy?
William A White
Ranch Hand

Joined: Jul 01, 2004
Posts: 35
Well, you can certainly implement a clone() method that makes a copy, or make a method that creates a new object altogether containing the data you need presented.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The Java idiom for copying objects is to first implement the Cloneable interface and then override the clone() method. The premordial Object class provides a default clone(), but it only performs a "shallow" copy. In order to do a "deep" copy, you need to override clone(). See the JavaDocs for Object and Cloneable for more details.

Layne


Java API Documentation
The Java Tutorial
Pete Neu
Ranch Hand

Joined: Feb 18, 2005
Posts: 86
That was exactly my question in the first place. How
can I do this? The problem is: I need a deep copy. Because
the databaseVO conatains tablesVO which in turn contain attributeVO.

So what's the best approach in this case?
Pete Neu
Ranch Hand

Joined: Feb 18, 2005
Posts: 86
Ok, Layne.
I will try this.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Pete Neu:
I usually stick to this method as well. But in this case a lot
of meta-data needs to be displayed and the filter logic is so complex
that I really need to modify the data otherwhise it would lead to
really confusign methods.


To me, it looks like the code for formatting the data would be exactly the same wether you store it in a DAO, or just calculate it on the fly every time you need to display it, wouldn't it?

The problem with the former approach is that it can lead to nasty inconsistencies between the data displayed and the data actually in the system (the Liar View bug pattern). I'd rather go with the latter approach.


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
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Pete Neu:
That was exactly my question in the first place. How
can I do this? The problem is: I need a deep copy. Because
the databaseVO conatains tablesVO which in turn contain attributeVO.

So what's the best approach in this case?


Are you asking how to do the deep copy?


James Carman, President<br />Carman Consulting, Inc.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Pete Neu:
That was exactly my question in the first place. How
can I do this? The problem is: I need a deep copy. Because
the databaseVO conatains tablesVO which in turn contain attributeVO.

So what's the best approach in this case?


It sounds like each of these classes also need to implement Cloneable and override clone(). Then the clone() method of your Value Object will call each clone() on each of the member reference variables in turn.

I just noticed another post about Value Object and realized that this is a design pattern. I'm not familiar with this pattern, so there may be other issues involved that the rather straight-forward cloning paradigm in Java. You may want to look into this if it's an issue for your particular application.

Layne
Hung Yee
Greenhorn

Joined: Dec 22, 2004
Posts: 18
FYI: Sun has a tech tip article on making 'deep copies' of objects that might interest you:
http://java.sun.com/developer/JDCTechTips/2001/tt0410.html#making
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
That's what I would suggest. If your classes are serializable, you can serialize an object out to a byte array and then read it back in, thereby performing a deep copy. You might take a look at the Jakarta Commons Lang project and the SerializationUtils.clone() method for an example of how to do this. Or, just use that method! That's what reuse is all about!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
All good advice on cloning. I just still don't buy that cloning is the best solution to your problem...
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
I would agree. Cloning might not be the best option. In your situation, I would not modify (as someone else already said) the data for the presentation tier. But, I would maybe develop some sort of wrapper or helper object which can help you format the data and make calculations based upon it.
Reda Mokrane
Ranch Hand

Joined: Jul 25, 2001
Posts: 237
Here is one way to clone your VO.
Add a method in your VO.java that return a copy of your VO.


public Object cloneMyVO() {
YourVO clonedVO= new YourVO(this.youInstanceVariable1,
this.youInstanceVariable2);
return clonedVO;
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to clone Value Objects