Hi Lew,
Lew said:
Richard Maher wrote ... an
You would basically do this specifically to adhere to the "resource
acquisition is initialization" (RAII) idiom.
<
http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization>
Certainly looks similar.
I prefer personally to think of this as "destruction is resource release", but
the point is to tie the resource to the lifetime of the object itself.
However, one does not always follow this pattern; sometimes one prefers to
construct the object first, and acquire or release resources only from
fully-constructed objects.
The key is that the 'finally' be well placed.
This consideration does sometimes lead to putting resource management in
methods on fully-constructed objects, especially given the lack of explicit
destructors in Java. It depends on the resource, too. A socket may be better
handled in a constructor (perhaps with a last-ditch cleanup in 'finalize' as
discussed elsewhere in this thread). A GUI should not be started in the
constructor, although it should be constructed in the constructor.
Why not? Hey I don't like the idea at all either (especially if (or rather
when in my case
the indefinite user interaction is inside a
synchronized block) Sounds bizarre I know but this is what's on the
whiteboard at the moment: -
An Applet instance will ask for a Session class from the static
SessionRegistry. If a Session object already exists for that combination of
ApplicationName/Host/Port etc then that is the one that will be chosen,
otherwise a new Session will be manufactured. Now Connection is an
inner-class of Session. The Connection constructor just creates a Socket and
works out if it's SSL or not. There are also open() and close() methods for
Connection that are invoked from the Session constructor. Furthermore after
a connection to a remote host has been made a Connection.handshake() must
take place with a username/password. If authorization fails then the
(network) connection must be torn down. (There with also be a Reader() inner
class with a thread that will read the Socket and hopefully dispatch the
messages to the corrct JSObject)
Anyway the relevant bit to your post is that somewhere in there a user will
have to be quized for a Username/Password. The Applet that's trying to
instantiate Session doesn't really care if another Applet instance has
already succesfully authorized client access on that Socket, or that a
Session instance may exist but the Connection and Reader classess have been
torn down 'cos there were no longer any pages referencing the Applet; it
just wants to say "gimme a Session for Application X, at my codebase() and
port number 1234, so that I can do some network i/o for my fat 'n' sexy RIA
client".
Now you might say that the Applet itself should do the synchronization and
be responsible for checking if authorization is needed via an isAuthorized()
and/or isConnected() mechanism, but the whole point is that the Socket be
connected/authorized only once per all pages/tabs (and where possible
browser instances). If that prevents firing up any additional pages for a
given application until they've entered the Username/Passsword for the first
page they brought up, then I'm good with it.
So is there a physical Java barrier(s) preventing the use of GUIs
EventListeners et al (at least a couple of modal dialogue boxes) from a
constructor or is it just considered "bad form"?
The choices hinge on how one ties lifetimes together - the lifetime of the
object and the lifetime of the resource.
Fair enough.
Regards Richard Maher