For Programmers: Free Programming Magazines  


Home > Archive > Java Help > May 2004 > Re: object LifeForm composed of immutable object Location. how do I give a LifeForm









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: object LifeForm composed of immutable object Location. how do I give a LifeForm
javac

2004-05-04, 4:03 pm

"Ryan Stewart" <zzanNOtozz@gSPAMo.com> wrote in message news:<JYOdncGFbfFPMAjdRVn-hw@texas.net>...
[..]
> // Inside some method
> LifeForm alien = new LifeForm();
> alien.setLocation(new Location(1, 2, 3));
> Location alienLocation = alien.getLocation();
> Bomb nuke = new Bomb();
> nuke.setLocation(alienLocation);
>
> Obviously I took a couple of creative liberties there. The point is that
> since a Location is immutable, you can safely assign the same Location to
> two different objects. If a Location could be modified, and you assign one
> Location to both the alien and the bomb, then when that Location is
> modified, it affects both bomb and alien (so the alien can never get away
> from the bomb :). Obviously if things move around a lot, you're creating
> some overhead by constructing and throwing away a bunch of Location objects.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^

agreed, but performance isn't a present concern. absolutely, i'm
trying to wrap my head around OO programming. i _want_ to be
"constructing and throwing away a bunch of Location objects," just
dunno how to do that :)

here's what I presently have:
private static void moveLife(int i) {
System.out.println("..moveLife..");
lifeForm = (LifeForm)VECTOR_OF_LIFE_FORMS.get(i);
lifeForm.setLocation(1, 1, 1);
VECTOR_OF_LIFE_FORMS.set(i, lifeForm);
}//moveLife

I notice you use
alien.setLocation(new Location(1, 2, 3));
while i use
lifeForm.setLocation(1, 1, 1);

clearly different. I'll have to try your suggestion at home. please
note, i'm using static factory methods, so i wouldn't use "new," but
still food for thought.

javac@mail.com
source code: http://www.geocities.com/cjavacjava/
(now with actual links!)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com