1) What have we done?
We just add simple "Logger" object with method "log" like you can see in the following code:
public class Logger
public void log(String logMessage)
DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm ")
) + logMessage
2) How we apply it?
In Client we inject "Logger" for receiver and then we can log particular calling methods.
public class Client
public static void main(String args)
Receiver receiver = new Receiver();
Logger logger = new Logger();
public class Receiver
protected Logger logger;
public void doA()
System.out.println("Receiver doing A");
logger.log("Class Receiver executed method doA");
public void doB()
System.out.println("Receiver doing B");
logger.log("Class Receiver executed method doB");
public void setLogger(Logger logger)
this.logger = logger;
3) What are cons?
We just littering "Receiver" with this logging. We definitely want to stay class untouched.
4) How can we do this?
Let's wrap calling of "doA" and "doB into "Command" objects.