|
| "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!)
|
|