Solution: Direct access to Printer I/O lines

Christian Tanzer tanzer at swing.co.at
Sun Dec 17 04:33:39 EST 2000


Carel Fellinger <cfelling at iae.nl> wrote:

> Alex Martelli <aleaxit at yahoo.com> wrote:
> ...
> > This gives me a good chance for one of my favourite rants,
> > '"Singleton" design-pattern considered harmful'!-)  But
> > 'Singleton' is not being ideally applied, so I'll skip that.
> 
> And in an other thread you wet my appetite and direct me here!
> So let us hear that rant, mister, and enlight us mere mortals
> who still think that there is legitimate use of this singleton
> pattern.

Whether something is a singleton is highly dependent on (the scope of)
the application. If you want to have an evolvable and/or reusable
solution it might be better not to use the singleton pattern. 

Often just a tiny shift in your (or your customer's) point of view
suffices to convert an obvious case of singleton-ness into a situation
where more than one object is needed. Even in the case of a shared
resource the singleton pattern fails if you discover the need of
supporting more than one instance of the resource in question (e.g.,
two instead of one printers).

A random example: some time ago I implemented a class Command_Line
tackling the same problem as the getopt module but providing a nicer
interface. It seems obvious that you cannot have more than one
instance of this class per application. Some time later I found it
too difficult to try to teach command line interfaces to Windows users
and implemented a class Script_GUI which uses the Command_Line
instance of a python script to provide a WIMP interface for the
script. Of course, Script_GUI needs a Command_Line instance of its
own.

In my experience, the number of possible instances of a class is not a
property of the class but of the application where the class is used.
Putting a restriction on the number of instances into the class
therefore decreases the reusability of the class and makes life very
unpleasant if the original application must be changed to accommodate a
different number of instances.

Although I've held this view for a number of years I occasionally
succumb to the lure of the singleton pattern and more often than not I
regret it later on.

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list