Given an interface:
public interface Logger {
void log(String message);
}
Rather than usage:
public void doJob(Logger logger) {
if (logger != null) {
logger.log("[doJob]:Step 1");
}
// ...
if (logger != null) {
logger.log("[doJob]:Step 2");
}
// ...
if (logger != null) {
logger.log("[doJob]:Step 3");
}
}
public void doJob() {
doJob(null); // Without Logging
}
Because null objects have no state, it makes sense to use a enum singleton for it, so given a null object implemented like so:
public enum NullLogger implements Logger {
INSTANCE;
@Override
public void log(String message) {
// Do nothing
}
}
You can then avoid the null checks.
public void doJob(Logger logger) {
logger.log("[doJob]:Step 1");
// ...
logger.log("[doJob]:Step 2");
// ...
logger.log("[doJob]:Step 3");
}
public void doJob() {
doJob(NullLogger.INSTANCE);
}