|
9 | 9 | --- |
10 | 10 |
|
11 | 11 | ## Intent |
12 | | -Callback is a piece of executable code that is passed as an argument to other code, which is expected to call back |
13 | | -(execute) the argument at some convenient time. |
| 12 | + |
| 13 | +Callback is a piece of executable code that is passed as an argument to other code, which is |
| 14 | +expected to call back (execute) the argument at some convenient time. |
14 | 15 |
|
15 | 16 | ## Explanation |
16 | 17 |
|
17 | 18 | Real world example |
18 | 19 |
|
19 | | -> We need to be notified after executing task has finished. We pass a callback method for the executor and wait for it to call back on us. |
| 20 | +> We need to be notified after executing task has finished. We pass a callback method for |
| 21 | +> the executor and wait for it to call back on us. |
20 | 22 |
|
21 | 23 | In plain words |
22 | 24 |
|
23 | 25 | > Callback is a method passed to the executor which will be called at defined moment. |
24 | 26 |
|
25 | 27 | Wikipedia says |
26 | 28 |
|
27 | | -> 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; that other code is expected to call back (execute) the argument at a given time. |
| 29 | +> In computer programming, a callback, also known as a "call-after" function, is any executable |
| 30 | +> code that is passed as an argument to other code; that other code is expected to call |
| 31 | +> back (execute) the argument at a given time. |
28 | 32 |
|
29 | 33 | **Programmatic Example** |
30 | 34 |
|
@@ -61,21 +65,23 @@ public final class SimpleTask extends Task { |
61 | 65 | } |
62 | 66 | ``` |
63 | 67 |
|
64 | | -Finally here's how we execute a task and receive a callback when it's finished. |
| 68 | +Finally, here's how we execute a task and receive a callback when it's finished. |
65 | 69 |
|
66 | 70 | ```java |
67 | 71 | var task = new SimpleTask(); |
68 | 72 | task.executeWith(() -> LOGGER.info("I'm done now.")); |
69 | 73 | ``` |
70 | 74 |
|
71 | 75 | ## Class diagram |
| 76 | + |
72 | 77 |  |
73 | 78 |
|
74 | 79 | ## Applicability |
| 80 | + |
75 | 81 | Use the Callback pattern when |
76 | 82 |
|
77 | 83 | * when some arbitrary synchronous or asynchronous action must be performed after execution of some defined activity. |
78 | 84 |
|
79 | 85 | ## Real world examples |
80 | 86 |
|
81 | | -* [CyclicBarrier](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html#CyclicBarrier%28int,%20java.lang.Runnable%29) constructor can accept callback that will be triggered every time when barrier is tripped. |
| 87 | +* [CyclicBarrier](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html#CyclicBarrier%28int,%20java.lang.Runnable%29) constructor can accept a callback that will be triggered every time a barrier is tripped. |
0 commit comments