开发者

Calling a method in a class in each other method

开发者 https://www.devze.com 2023-02-27 06:45 出处:网络
I have a method that I want to be called each time one of 开发者_开发百科the other methods is called.I dont want to have to explicitely call it each time.Is there a way to have the other methods natur

I have a method that I want to be called each time one of 开发者_开发百科the other methods is called. I dont want to have to explicitely call it each time. Is there a way to have the other methods naturally call that method before executing its own code?

If I have a method thats called isThere() and I want it called in each other method, I dont want to have isThere() written in each method. I was hoping there would an easier way to do this.


You should look into AOP - Aspect Oriented Programming.

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


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


You may take a look into Observer pattern, which may also solve your problem a bit differently. Read the Wikipedia page linked here. Alternatively, you can read the Observer pattern from the Head First Design Pattern book.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号