   Design patterns--only one instance of a class
  I want to output some text to a log file through my program, and I wrote a logstream class complete with overloaded operators. Now, you will agree that there should be only one instance of a class like this: the constructor opens the filestream at program start-up, and closes it when the program quits.How do I go about a design pattern like this? I have read about Singletons, but I have also read that they should be used with caution, etc etc. My initial idea was something like std::clog or std::cout ... how can something like that be implemented?   
  That's a prime example of where to use a singleton..Even the "Singleton-haters" agree on that..   ;  Yup, you should use a singleton pattern for that. People say that singleton should be used with caution, not that it's an awfull demon that should be banned from programming ^^   ;  std::cout etc are simply global objects.   ;  May I ask why there must be only one instance?   ;Quote:Original post by paicPeople say that singleton should be used with caution, not that it's an awfull demon that should be banned from programming ^^Actually some people (I will name no names) say just that. [wink]   ;Quote:Original post by bakery2k1std::cout etc are simply global objects.I actually did see the <iostream> header - cout, cerr, clog are all declared as extern, but where are they actually defined? I would really like to go with something like this. I could create a global object, but how do I call the constructor so that it can open the ofstream?If I'm not happy or not successful with what bakery2k1 is suggesting, I will go along with trying to design a singleton (which I haven't done before).swiftcoder, I read the response to your article here at Gamedev, and would you believe it that's what completely put me off singletons! [lol]   ;Quote:I could create a global object, but how do I call the constructor so that it can open the ofstream?The constructor will be called for you when the global object is initialised.Quote:cout, cerr, clog are all declared as extern, but where are they actually defined?Probably in some library file somewhere.   ;  Use a Singleton...here's an example of how a Singleton works (behind the scenes)The singleton itself:public class Logger {    private Logger instance = null;   private Logger() {   // Create some file IO object or whatever.   // IMPORTANT: Note that the constructor is private.    }   public static Logger getInstance() {   // If an instance of the object has not been created,   // then it will be created using the following line.   if (instance == null) instance = new Logger(); // Return the new or existing Logger "instance".   return instance;    }   public void log(String info) { ... }}Use of the singleton:public class GameEngine {    private Logger logger = Logger.getInstance();   private someMethod() {   logger.log("I'm in someMethod().");    }};Quote:Now, you will agree that there should be only one instance of a class like this: Absolutely not. What if [heaven forbid] you want to log rendering info to one file and sound info to another?Don't limit your options.[edit: to be more helpful, I think the singleton might have it's place, but logging is definately not it. I find that logging is much better implimented as a strategy pattern]   