File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Design for Dirty Check	Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Design for Dirty Check	" Watch "Design for Dirty Check	" New topic

Design for Dirty Check

Arundhathi Menon
Ranch Hand

Joined: Jan 14, 2004
Posts: 113
What would be a smart of checking if an object is dirty especially if am looking at only a subset of its properties for a change

Eg. class Person should be have a field updated if and only if 24 specific properties out of its 30 properties are modified

instead of defining a method like

private boolean hasChanged(Person p1 , Person p2){
if(!p1.getAttr1().equals(p2.getAttr1()) ||
!p1.getAttr2().equals(p2.getAttr2()) ||
!p1.getAttr3().equals(p2.getAttr3()) ...
return true;
return false;

I dont want to use reflection just to accomplish this. Is there a better design for checks like these?


Greg Charles

Joined: Oct 01, 2001
Posts: 2967

First of all, I don't like the name hasChanged() here, since you are actually comparing two different instances. A hasChanged() method implies that a single instance has changed its state since the last checkpoint, where checkpoints are defined in some way meaningful to your system. You could accomplish that sort of check by having every set method also flip on a "dirty" flag in the instance. In fact, we had to do something like this in the very early days of ORM, even with Entity Beans from EJB 1.0 if I remember right. Of course, you'll also have to have some way to turn off the dirty flag whenever that makes sense for your application.

What you've implemented looks more like an equals() method. Unfortunately, there's no better way to do that other than just writing out the code, but some IDEs have tools that will do the typing for you. I don't understand the 24 out of 30 requirement. Could you be clearer about what you're trying to do?
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

You could create a Set for the specific properties.
Everytime the setAttr() methods is called on an attribute add it to the Set.
When the size of the Set is 24 you have all 24 attributes changed.

The name of the attribute can be used as the object for the Set.

When the time is right you can remove all the elements from the Set.

This approach is different from what you wanted.
But it can be a workaround.

Hope this is helpful.

Arundhathi Menon
Ranch Hand

Joined: Jan 14, 2004
Posts: 113
Thanks Greg & Preity.
Greg , you are right , what am comparing is the UI entity versus its old copy that was stored in the DB. They are different class types for sure , but have the same set of properties.
By 24 out of 30 I meant the dirty flag had to be set only when certain properties are updated and not all.

Preity , thanks for the input. I was trying to get to a generic way of coding this versus writing it all out in a block of If statement with multiple OR conditions.

Looks like the only way out.

Thanks again
David Newton

Joined: Sep 29, 2008
Posts: 12617

Eg. class Person should be have a field updated if and only if 24 specific properties out of its 30 properties are modified

That isn't what your code indicates--your code is checking to see if *any* of the 24 specific properties have been modified, not iff 24 specific properties have been modified.

If the code is correct, the same thing could be done by creating a hash value of those 24 properties and comparing that against the hash of the possibly-modified object.
I agree. Here's the link:
subject: Design for Dirty Check
It's not a secret anymore!