budi darmanto
budi darmanto

Reputation: 1

How method from implement class can be used to catch event in Java?

For example: doInBackground, postExecute, preExecute in android AsyncTask. If I press on a button in my apps, it calls doInBackground method.

How did it happen?

Upvotes: 0

Views: 45

Answers (1)

Troy Young
Troy Young

Reputation: 191

This is a very common design pattern named callback. Check it out: https://en.wikipedia.org/wiki/Callback_(computer_programming)

A callback is often back on the level of the original caller. In computer programming, a callback, also known as a "call-after" function, is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time. This execution may be immediate as in a synchronous callback, or it might happen at a later time as in an asynchronous callback. In all cases, the intention is to specify a function or subroutine as an entity[clarification needed] that is, depending on the language, more or less similar to a variable (see first-class functions).

Also you can refer the example here: https://github.com/iluwatar/java-design-patterns/tree/master/callback/src/main/java/com/iluwatar/callback

Upvotes: 2

Related Questions