Re: static or not?




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.

.



Relevant Pages

  • Re: In search of a better line driver
    ... >the package to drive several others in parallel, ... >gates are used, lot variations in the output impedances of the gates can ... The thing is so small I can hardly solder it. ... I have a little general purpose line driver board I made using ...
    (sci.electronics.design)
  • In search of a better line driver
    ... gates are used, lot variations in the output impedances of the gates can ... I haven't tried using only a 74HC14 package with some parallel gates, ... I have a little general purpose line driver board I made using ... same perhaps with the Fairchild chips. ...
    (sci.electronics.design)
  • Re: Another Linux Myth Exposed - Hopeless at playing music ..
    ... But I can't get sound working on either machine. ... and says it is configured with driver snd-emu10k1. ... I look at the descriptions in the package manager at ... the moment and it takes care of the dependencies. ...
    (alt.os.linux.suse)
  • Re: Image pushing failing after minint folder has been installed.
    ... Initially I did have that problem but once I pointed the package to the ... like windows PE doesn't have the correct driver. ... nic driver to the following folders in the Customwinpe folder in my sms ...
    (microsoft.public.sms.tools)
  • Re: New to Linux, not to *nix, having problems xdm, ups, ntp
    ... I can't get xdm or gdm to run on the local console, ... How can I check if the nv driver is loaded? ... >> open the embedded JAR files), and when I try the rpm package, rpm ... but I'm guessing none of them actually install the package. ...
    (alt.linux)