Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a callback. <a href="http://en.wikipedia.org/wiki/Callback_%28computer_programming%29">According to Wikipedia</a>:</p> <blockquote> <p>In computer programming, a callback is a reference to a piece of executable code that is passed as an argument to other code.</p> </blockquote> <p>So let's look at the executable code:</p> <pre><code>public void getHelp(HelpCallback callback){ //do something callback.call(OK); } </code></pre> <p>Here, the <code>callback</code> argument is a reference to an object of type <code>HelpCallback</code>. Since that reference is passed in as an argument, it is a callback.</p> <h1>An example of delegation</h1> <p>Delegation is done internally by the object - independent of how the method is invoked. If, for example, the <code>callback</code> variable wasn't an argument, but rather an instance variable:</p> <pre><code>class MyDriver { public static void main(String[] argv){ // definition of HelpStrategy omitted for brevity MyObject myObj = new MyObject(new HelpStrategy() { @Override public void getHelp() { System.out.println("Getting help!"); } }); myObj.getHelp(); } } class MyObject { private final HelpStrategy helpStrategy; public MyObject(HelpStrategy helpStrategy) { this.helpStrategy = helpStrategy; } public void getHelp(){ helpStrategy.getHelp(); } } </code></pre> <p>... then it would be delegation.</p> <p>Here, <code>MyObject</code> uses the <a href="http://en.wikipedia.org/wiki/Strategy_pattern">strategy pattern</a>. There are two things to note:</p> <ol> <li>The invocation of <code>getHelp()</code> doesn't involve passing a reference to executable code. i.e. this is not a callback.</li> <li>The fact that <code>MyObject.getHelp()</code> invokes <code>helpStrategy.getHelp()</code> is not evident from the public interface of the <code>MyObject</code> object or from the <code>getHelp()</code> invocation. This kind of <a href="http://en.wikipedia.org/wiki/Information_hiding">information hiding</a> is somewhat characteristic of delegation.</li> </ol> <p>Also of note is the lack of a <code>// do something</code> section in the <code>getHelp()</code> method. When using a callback, the callback does not do anything relevant to the object's behavior: it just notifies the caller in some way, which is why a <code>// do something</code> section was necessary. However, when using delegation the actual behavior of the method depends on the delegate - so really we could end up needing both since they serve distinct purposes:</p> <pre><code> public void getHelp(HelpCallback callback){ helpStrategy.getHelp(); // perform logic / behavior; "do something" as some might say if(callback != null) { callback.call(); // invoke the callback, to notify the caller of something } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload