#include <iostream>
#include <stdarg.h>
using namespace std;
class ILogger
{
public:
virtual void WriteLog(string format, ...) = 0;
};
class LocalLogger: public ILogger
{
public:
void WriteLog(string format, ...)
{
va_list argptr;
va_start(argptr, format);
char buffer[1024] = {0};
vsprintf(buffer, format.c_str(), argptr);
va_end(argptr);
cout << "WriteLocalLog:" << buffer << endl;
}
};
class DbLogger: public ILogger
{
public:
void WriteLog(string format, ...)
{
va_list argptr;
va_start(argptr, format);
char buffer[1024] = {0};
vsprintf(buffer, format.c_str(), argptr);
va_end(argptr);
cout << "WriteDbLog:" << buffer << endl;
}
};
int main()
{
ILogger* pstLocalLogger = new LocalLogger();
if (nullptr == pstLocalLogger)
{
cout << "New logger failed." << endl;
return -1;
}
pstLocalLogger->WriteLog("local %s.", "content");
delete pstLocalLogger;
pstLocalLogger = nullptr;
ILogger* pstDbLogger = new DbLogger();
if (nullptr == pstDbLogger)
{
cout << "New logger failed." << endl;
return -1;
}
pstDbLogger->WriteLog("db %s.", "content");
delete pstDbLogger;
pstDbLogger = nullptr;
return 0;
}
编译执行
PS E:\workspace\sample\Adapter> g++ adapter.cpp
PS E:\workspace\sample\Adapter> .\a.exe
WriteLocalLog:local content.
WriteDbLog:db content.