Re: static or not?
- From: iamfractal@xxxxxxxxxxx
- Date: 25 May 2005 00:56:18 -0700
Hi, Thufir,
Well, I'm not sure what you mean by a, "Driver," class, so I'll take a
guess a guess at a definition: a Driver class is a class that
coordinates a sequence of interactions with a number of other classes.
(If that's not what you meant, of course, then the rest of this post
is irrelevant.)
I did not mean to imply that I would automatically make any such
Driver class a singleton. Several instances of such a class may be
required, in which case my reply to the OP does not hold.
If the application did, however, require one and only one Driver
object to be instantiated, then I would have a small preference to
using a singleton as opposed to a class of static methods, if this
Driver was not accessible outside its package.
On a system-level, the package is generally the primary unit of
encapsulation, as opposed to the class (though of course even
class-level behaviour should be as encapsulated as possible);
afterall, if you were presented with a presentation on a new system,
would you rather be first presented with a web of 5,000 classes, or 40
packages?
Such being the case, the main reason for this small preference for a
singleton would be the possibility that the Driver might, some day, be
made accessible outside its package. While Driver is only visible
inside one package, then having all the classes depend on the Driver's
concrete class is not too big a problem, because the reason all these
classes are packaged together is (presumably) that they have some
commonality of purpose, for example, they all work to put a GUI on a
screen. Thus it's possible that if there are new requirements on the
GUI, the implementation of many classes within that package might be
impacted, and the degree to which they can all be hidden from one
another is limited.
If, however, the day comes that the Driver must be accessible outside
the
package, then there will suddenly be a strong preference for having
Driver implemented as a singleton rather than a class of static
methods.
If we continue with out example, the Driver that controls the
display of the GUI is now being depended on by another package which
(presumably) has nothing to do with GUI: it could, for example, be a
Database class that wants to output a query result, but doesn't care
whether
the output is made to a GUI, a socket, a text file, etc.
If Driver were a class of static methods, then Databse would now need
a dependency on the concrete class Driver. And as Driver only controls
the GUI, then if Database wants to output to a text file, it must have
some knowledge of other functionality that writes its output to that
medium.
If Driver were a singleton, however, then, as mentioned, it could
declare an interface (View) that is registered in a Registry that is
known to Database. But all the other means of outputting data could
also register that same interface to implement functionality for their
own, specific medium. Thus TextView, FileView, and SocketView
could all register their View interface in the Registry.
And when Database wants to write a result, it can ask Registry to
return the currently-selected output medium implementation, hidden
behind the View interface. This current selection could be controlled
by a user option. Also, if there are new requirements on the GUI, such
as the requirement that output should be made via Email to the CEO,
this new view can be introduced to the system(e.g., EmailView) without
impacting the Database functionality.
Now, I'm just waiting to see, "No, that's not what I meant by Driver
...."
..ed
www.EdmundKirwan.com - Home of The Fractal Class Composition.
.
- Follow-Ups:
- Re: static or not?
- From: hawat.thufir@xxxxxxxxx
- Re: static or not?
- References:
- static or not?
- From: David Vanderschel
- Re: static or not?
- From: iamfractal
- Re: static or not?
- From: hawat.thufir@xxxxxxxxx
- static or not?
- Prev by Date: import Tidy jar, path to jar
- Next by Date: Re: Peephole optimizer for Java class files
- Previous by thread: Re: static or not?
- Next by thread: Re: static or not?
- Index(es):
Relevant Pages
|