jeudi 6 avril 2023

Execute a method right before execution of various methods in different classes using Java

I am facing a code design challenge and not very sure how to resolve it. Tried researching a bit on the net but that made me even more confused.

InterfaceI1 -> Parent level interface (Single method)

Class C1 -> C1 implements I1 (C1 overrides method from I1 has no method of its own)

Class CC1, CC2, CC3, CC4, CC5 -> All these extend C1 (Overrides method in C1 and has methods of its own as well)

The problem is : I need to execute a piece of code on execution of certain methods in classes CC1, CC2, CC3, CC4, CC5. These methods have nothing in common and certainly its not the overridden method.

Is there a way to write this common piece of code in a central place and make it execute on execution of concerned methods?

Also the execution of this common piece of code would also depend on another environmental condition. For eg :

If the method in CC1 is executed && its a weekend { Execute the common code }

Since i dont want to clutter all the methods with the same boilerplate code i did some research and came across AOP. It looked like a fit for the requirement though not sure if conditional execution could be managed with AOP. As i was reading about it i learned about Action At Distance anti-pattern and how its not good for the code manta inability.

Any leads or suggestions would be helpful.

Edit : CC1, CC2, CC3, CC4 is not a spring bean

Aucun commentaire:

Enregistrer un commentaire