R
Roedy Green
I was reading a book called Java Platform Performance, Strategies and
Tactics.
It pointed out the bottleneck often turns out to be creating zillions
of tiny objects. The author was on the Swing team profiling it to
reduce the number of objects generated.
That is why Swing Renderers reuse the same Component over and over.
Anyway, consider a method LIKE getSize that returns a Dimension
object.
getSize creates a new Dimension object every time so that if the user
screws with the fields, it won't affect anyone else.
Ah ha you say. Why not invent an immutable Dimension object, then you
can safely return the same copy to everyone?
Well, what if you want to change the value and have everyone informed
who has taken a reference to the immutable object?
you can SUBCLASS the immutable object adding setter methods. You
return an reference to the "immutable" object, but you set it with the
mutable object reference.
Now the sneaky user could subvert you by casting to the mutable
version. Unfortunately, you can't hide the mutable version. You can't
restrict access when you subclass.
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
Tactics.
It pointed out the bottleneck often turns out to be creating zillions
of tiny objects. The author was on the Swing team profiling it to
reduce the number of objects generated.
That is why Swing Renderers reuse the same Component over and over.
Anyway, consider a method LIKE getSize that returns a Dimension
object.
getSize creates a new Dimension object every time so that if the user
screws with the fields, it won't affect anyone else.
Ah ha you say. Why not invent an immutable Dimension object, then you
can safely return the same copy to everyone?
Well, what if you want to change the value and have everyone informed
who has taken a reference to the immutable object?
you can SUBCLASS the immutable object adding setter methods. You
return an reference to the "immutable" object, but you set it with the
mutable object reference.
Now the sneaky user could subvert you by casting to the mutable
version. Unfortunately, you can't hide the mutable version. You can't
restrict access when you subclass.
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes