
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
On to User Interfaces
|
385
cannot meaningfully extend a class used through its instances. If Logger were used
entirely through class methods, it could not inherit the functionality of the
Observable class because Observable must be instantiated to work. The inverse is
also true: a class comprised solely of class members is difficult to extend (sometimes
prohibitively so) because of the scope issues described in Chapter 6.
The Singleton pattern gives us the benefits of centralized access to some aspect of a
program without the limitations of class methods and class properties.
A Warning Against Singletons as Globals
Beware that the Singleton pattern can turn an object-oriented program into a series
of haphazard global functions and variables, negating most of the benefits of OOP.
You should use the Singleton pattern when you need to limit the number of
instances created by a class, not just when you want to access something globally. If
you are tempted to use a Singleton simply as a repository for global variables, you
should stop, rethink the problem, and try to fit the services you’re implementing into
some logically encapsulated structure with the rest of your program. When code is
spread loosely around in global variables and functions, it’s much harder to main-
tain, extend, and