Assuming a abstract class:
class ILogger {
virtual ~ILogger() = default;
virtual Log(const std::string&) = 0;
};
Instead of
void doJob(ILogger* logger) {
if (logger) {
logger->Log("[doJob]:Step 1");
}
// ...
if (logger) {
logger->Log("[doJob]:Step 2");
}
// ...
if (logger) {
logger->Log("[doJob]:End");
}
}
void doJobWithoutLogging()
{
doJob(nullptr);
}
You may create a Null Object Logger:
class NullLogger : public ILogger
{
void Log(const std::string&) override { /* Empty */ }
};
and then change doJob
in the following:
void doJob(ILogger& logger) {
logger.Log("[doJob]:Step1");
// ...
logger.Log("[doJob]:Step 2");
// ...
logger.Log("[doJob]:End");
}
void doJobWithoutLogging()
{
NullLogger logger;
doJob(logger);
}