Calling a method in a class in each other's method

I have a method that I want to call every time one of the other methods is called. I do not want to call him every time. Is there a way for other methods to naturally call this method before executing native code?

If I have a method called isThere (), and I want it to be called in another method, I don't want to have isThere () written in each method. I was hoping there would be an easier way to do this.

+6
source share
3 answers

You should take a look at AOP - Aspect Oriented Programming.

Since you are using Java, I recommend you take a look at AspectJ .

+5
source

You can access all other methods using another method that first calls there and then uses the parameter that you passed to it to determine which method you want to use, using a switch statement or something similar.

0
source

You can take a look at the observer pattern , which can also solve your problem a little differently. Read the Wikipedia page related here. Alternatively, you can read the Observer pattern from the Head First Design Pattern.

0
source

Source: https://habr.com/ru/post/886257/


All Articles