The Artima Developer Community
Sponsored Link

Java Community News
Defining Object Identity

4 replies on 1 page. Most recent reply: Aug 3, 2006 12:35 AM by Stefano

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Defining Object Identity Posted: Jul 28, 2006 8:26 AM
Reply to this message Reply
Summary
In a recent java.net article, Peter Becker examines notions of object identity in Java, including identity in terms of memory location, business identity, and database identity, and suggests that developers define equals() in a way that avoids references to mutable fields.
Advertisement

Notions of Java object identity are well defined in the JDK docs, and most developers understand the difference between == and equals(). However, identity is a more complex issue in real-world applications, as Peter Becker's recent java.net article, You Are What You Is: Defining Object Identity, observes. He distinguish between notions of identity in the following contexts:

  • Keeping identity when your program stops
    What happens with objects that are needed after you shut down a program and restart it? Objects usually get serialized and then get deserialized when they are needed again...

  • Having objects represent external data
    Objects can come from external sources, such as a relational database. The structures on which these objects are based often define their own identities such as using primary keys to identify rows in a table.

  • Having identifiers in the business world
    What happens if ... the business side already has a strong notion of how to identify entities? Order numbers, passport IDs, and social security numbers are examples of this...

Becker proposes that you:

Make sure that the way a program understands the identity of objects is the same as the notion of identity that comes out of the business context.

He then proceeds to distinguish between value objects and state objects in defining identity, and illustrates implementing equals in a way that relies on a business notion identity. He also shows why equals() should not rely on mutable fields in the case of value objects:

[It] can be quite useful to completely distinguish state objects and value objects. A state object is an object that stores mutable state and is identified through reference equality, very much a standard Java object with getters and setters. A value object is always immutable and is identified through value equality. In Java that means overriding equals() and using only private members with no setters.

What do you think of Becker's notions of defining identity? Especially in a database application, what are your preferred ways of defining object identity?


Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Defining Object Identity Posted: Jul 28, 2006 11:10 AM
Reply to this message Reply
The idea of only checking non-mutable elements in equals() is interesting.

This article is a bit more support for Allan Holub's theories. see:

http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html

Ivan Lazarte

Posts: 91
Nickname: ilazarte
Registered: Mar, 2006

Re: Defining Object Identity Posted: Jul 28, 2006 4:24 PM
Reply to this message Reply
i hinted at it in the original thread, but for me, data is inherently transient, thus equality will always be a simpler solution. database identity, imo, is an unnecessary layer of complication since data exists as an abstraction above the original store. i think in the 4 or so years of web development work, the famous identity vs. equality bug has never creeped into my code or the code of people i've worked with.

to each his own?

Marcin Kowalczyk

Posts: 40
Nickname: qrczak
Registered: Oct, 2004

Re: Defining Object Identity Posted: Jul 29, 2006 2:36 AM
Reply to this message Reply
> The idea of only checking non-mutable elements in equals() is interesting.

This idea is older than Java. http://citeseer.ist.psu.edu/baker93equal.html

Stefano

Posts: 2
Nickname: zazzarone
Registered: Jan, 2004

Re: Defining Object Identity Posted: Aug 3, 2006 12:35 AM
Reply to this message Reply
Very Interesting discussion... Kevlin Henney on http://www.curbralan.com/ has discussed the need of precise design on Value Object on more documents. Very important, about me, is the paper "Valued Idioms: Patterns for Value-Based Programming" in which design of Value Object derive from a discussion about main features of concept of Object: identity, state, behaviour...

Flat View: This topic has 4 replies on 1 page
Topic: Defining Object Identity Previous Topic   Next Topic Topic: Ajax4JSF Reaches 1.0 Milestone

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use