Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Alosh Bennett is a passionate programmer and has been working on various java technologies since 2002. He is an ardent believer and promoter of the FOSS cause and is interested in RESTful architectures, web development and scripting languages. He is currently employed with Oracle Corporation. Alosh is a DZone MVB and is not an employee of DZone and has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

Wish List: Java Object Replacement

12.27.2010
Email
Views: 4210
  • submit to reddit

I wish I could replace an object with another of the same type and the myriad references existing to the first one would automatically be redirected.

Obj o1 = new Obj("Red");
map.put("Ball", o1);
list.add(o1);
ball = o1;
Obj o2 = new Obj("Blue");
o1.replace(o2);

 

map.get("Ball") should return the blue ball.
list should contain blue ball.
ball == o2 should evaluate to true.

We can work around the situation by extra code in Obj using composition and method call redirection. Wouldn’t live be simpler if the support is native?

The security part could be controlled by providing a markable interface or annotation.

Real life examples? Substituting a player. Corporate mergers. Two pools joining to make a bigger pool.

From http://www.aloshbennett.in/weblog/2010/java/wish-list-java-object-replacement/

Tags:
Published at DZone with permission of Alosh Bennett, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Dennis Müller replied on Mon, 2010/12/27 - 9:25am

I do not think that this is really required. If replacement is required, a link-object can help here. Build n references to the link-object and one from the link-object to the real object. Replacement is then really easy. Furthermore you can control which link-object-references should be replaced. 

Lets take your first example. If I would exchange all links for a player with another one, his family may not be so happy about this. A role specific exchange would be better here: Create a role object which has the link to the player and is referenced wherever needed. The exchange of the player will be quite easy.

Sven Diedrichsen replied on Mon, 2010/12/27 - 10:14am

Why not use a proxy? see: http://en.wikipedia.org/wiki/Proxy_pattern

And if you don't want to create a proxy manually use this one here http://www.javaworld.com/jw-11-2000/jw-1110-proxy.html

Jonathan Fisher replied on Mon, 2010/12/27 - 10:54am

That would really change the semantics of how Java works... Not saying thatd be bad, but think of the millions getters/setters you've created of the number of years. This would really change whether or not they need to exist. There's also the whole security aspect, who would have control over the replace method?

Stephane Vaucher replied on Mon, 2010/12/27 - 11:52am in response to: cj91

Actually, I'll say it. It's a bad idea.

Cloves Almeida replied on Mon, 2010/12/27 - 12:20pm

Don't believe it's a good idea. You'd need to reference count every variable assignment - add a lot of overhead for little benefit. There are a lot of alternatives. You could copy object state or use proxy objects as Sven suggested. Or don't assign and reference the variable from it's source.

Cosmin Mutu replied on Tue, 2010/12/28 - 2:31am

Bad idea. If you came across such requirements, check your code, for sure it can be wrote better.

See first comment.

 

Eric Giese replied on Thu, 2010/12/30 - 2:58am

This effectively takes all safety and guarantees about (im)mutability and throws them out of the window.

If you want to replace an object, create a mutable container and put your object in (in the simplest case: an array). Like the first comment stated.

Alosh Bennett replied on Sat, 2011/01/01 - 11:20am

As all the comments point out and rightly so, wrapping the object in a mutable container gives you all the control to replace the object. Further, as David Muller points out, its possible to replace the reference on a role-based manner (thanks David).

I have a tree-like structure with a lot of references to its node stored through out the code. It is possible for the user to re-arrange this structure and when he does that, sometimes two nodes merge into a single one. All references to both the earlier nodes will now point to the new node

For most of the part, the nodes behave like a normal POJO. If I could replace the references, I need not write any boilerplate code and could keep the node class simple and clean. The feature could be controlled in several ways so that immutable objects remain immutable.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.