Skip to content
Advertisement

Identify the caller inside a method in Java

I have a class A and class B as below :

JavaScript

Now the myMethod() is same in both the class but the implementation for send() method is different for class A and B. I am trying to implement the myMethod() inside a Utility class to avoid the duplication of method logic.

JavaScript

But while calling the send(), how would I identify which object actually calls the method so that I can redirect to the appropriate send() method. Do I need the instanceOf in java or or what is a better approach for this problem.

Advertisement

Answer

This is an alternative solution to Anthony Raymond:

JavaScript

Another solution is to use Decorator Pattern:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement