Status Update: Death to the Singletons

2 views
Skip to first unread message

Gabriel M. Beddingfield

unread,
Dec 11, 2009, 1:58:05 PM12/11/09
to Composite Development

SUMMARY:

+ I'm almost done removing Singleton classes, with
only the Big One (Tritium::Engine) remaining.
When done, there will only be one Singleton in
libTritium (Tritium::Logger).

+ FYI, There is an RSS feed on Gitorious that shows
every time I push new code to the repository.

SINGLETONS:

A singleton class is a class where there can only ever be
ONE. (To read more about what a singleton is, see [1].) In
Tritium, they create barriers to reusability of code. For
example, it complicates issues when repackaging the Sampler
as a plugin.

The problem with liberal use of Singletons is that they
prevent you from reusing things that /could/ be reused. For
example, Tritium::Sampler was once a singleton. Is there
any reason why there should *only* be *one* Sampler? Same
with Tritium::Engine (formerly H2Core::Hydrogen). What if
we want to load Engine twice... for some unthought-of good
idea? (Like, i dunno... a plugin.)

Singletons also make it difficult to isolate and test
classes. You find yourself having to add in more and more
heavy-weight components in order to get your tests running.

In reality, the singleton classes were used simply to have
global variables... but without actually having global
variables (since global variables are considered evil).

To replace this, the GUI will now use a global variable
(Tritium::Engine* g_engine) and libTritium is adding
m_engine member variables with a pointer to the parent
Engine instance.

This should be completed in the next few days.

RSS FEED:

Gitorious provides RSS feeds for changes to projects, in
case you're interested. However, they are only providing
"Atom" feeds (which is not compatible with some RSS feed
readers).

Everything: http://gitorious.org/composite.atom
Just Code: http://gitorious.org/composite/composite.atom

Peace,
Gabriel

[1] http://en.wikipedia.org/wiki/Singleton_pattern
Reply all
Reply to author
Forward
0 new messages