Interview Question
Country: United States
class Logger {
private:
bool db_flag;
string log_name;
static Logger *m_pInstance;
Logger (){};
Logger(const Logger &);
Logger &( operator =(const Logger &) {};
public:
static Logger * Instance() {};
bool openlogfile(bool flag,string logname);
void writetoLog();
bool closeLog();
}
Logger * Logger::m_pInstance = NULL;
Logger * Logger :: Instance(bool flag,string logname) {
if (!m_pInstance)
m_pInstance = new Logger(flag,logname);
return m_pInstance;
}
bool Logger::openlogfile(bool flag,string logname) {
db_flag = flag;
log_name = logname;
if (db_flag){
//connect to DB
} else
open (logname);
}
Call in main :
Logger ::Instance()->openlogfile(bool true,"myDB");
Logger::Instance()->writetofile("message 1..");
I think we shall use Chain of responsibilities pattern..
- amidala.shiva November 07, 2014There can be different objects which are in chain, and one among them would serve the request..
For example, There is already an object which serves to write log to file and
add new object which serves writing log to Database..