Singleton Help!

Started by
23 comments, last by Tradone 18 years ago
Quote:Original post by Way Walker
I don't see a reason to not go with a global.

If you want some justification for not making it a singleton, just allow the user of the class to specify hich stream it should print to. This is useful and it's entirely possible you may want more than one Print object so you can print to multiple streams.

Really, "Singleton" is just a fancy name for "One and only one global instance". If everything needs access to a given instance of Print, that sounds like a job for a global. Why not make a global? Because down the line you may want different parts to print to different Print objects. Depending on exactly what you're printing, this may either be unlikely or the Print instance might easily and logically be grouped with other things you're passing around (e.g. as a member of some "world state" object).

However, I see no shame in using a global when it seems appropriate. Is the standard library poorly designed because std::cout is a global (or does tucking it in a namespace make it "less global"?)?


those are very powerful thoughts you put into words there.
I guess it was appropriate to use a global in that case.
But it seems like I've already implemented singleton, and I think I'm going to adventure and practice using singleton using this opportunity.

Valid Point!
Advertisement
well I was successful in implementing the singleton pattern, but I want to make it better. Please help me, for I have NO, not even a spec of backgrounds on templates and still unfamiliar with typedef with my unconventional education.
// singleton.h#ifndef __SINGLETON_H#define __SINGLETON_Htemplate <class T>class Singleton{public:  static T& Instance() {    static T _instance;    return _instance;  }private:  Singleton();          // ctor hidden  ~Singleton();          // dtor hidden  Singleton(Singleton const&);    // copy ctor hidden  Singleton& operator=(Singleton const&);  // assign op hidden};#endif// eof


okay, this is what I'm using right now.
and as described in the tutorial.

To transform the Log class from the beginning of the previous article to a singleton, without adding an Instance() function and hiding the constructor, destructor etc, I could define it this way:

typedef Singleton<Log> LOG;

And I can write to the log from any location in the application code (as long as it includes the necessary headers):

LOG::Instance().Write(“This is a logline”);

I typedef the object, and use "LOG::Instance()."
but I want to make it more convenient by somehow defining
LOG::Instance(). into just plain old log so that I can just
log.some_method();
instead of writing out the whole thing LOG::Instance().some_method();

thanks.
before I had all the print methods of each container within that container method. for example, Cookie::Print();

now I can reorganize them all inside the Print class
Print::Cookie( std::map<std::string, std::string>& para_map )

damn, this is so cool.
Now I'm reimplementing the Algorithms class, this class is a collection of algorithms that are used throughout the whole program, most classes seem to need it, and if some of those classes don't require it, then I can simply not include the header file and don't perform a typedef on the Algorithm singleton.

:D !!!

Thanks for your advices everyone.
151# g++ -o new_shenu.cgi new_shenu.cpp Algorithms.cpp Path.cpp Cookie.cpp Parameter.cpp
/var/tmp//ccmKyJcL.o(.text+0x197): In function `main':
: undefined reference to `Print::Parameter(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
/var/tmp//ccmKyJcL.o(.text+0x205): In function `main':
: undefined reference to `Print::Cookie(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
/var/tmp//ccmKyJcL.o(.text+0x276): In function `main':
: undefined reference to `Print::Path(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
/var/tmp//ccmKyJcL.o(.text+0x624): In function `main':
: undefined reference to `Print::Error(std::string)'
/var/tmp//ccmKyJcL.o(.gnu.linkonce.t._ZN9SingletonI5PrintE8InstanceEv+0x18): In function `Singleton<Print>::Instance()':
: undefined reference to `Print::Print()'
/var/tmp//ccJ2h5hH.o(.text+0x25b): In function `Algorithms::ParseData(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > >&, std::string)':
: undefined reference to `Print::Error(std::string, std::string)'
151#

do you know what this means?
I'm done reorganizing by singletons, but left with these incomprehensible linking errors.

This topic is closed to new replies.

Advertisement