Callbacks explained by hollywood principle

I thought of explaining the concept of callbacks via hollywood principle of : “Dont call us, we will call you, just leave your phone number (register callback) with us :)”

So, I made a small java program for the same.
Use case: SRK wants to apply for Hollywood movie.

1.) class HollywoodDirector : Hollywood director maintains the list of callbacks
(telephone numbers) of all the struggling actors.


/** 
 * @author anirudh 
 */

public class HollywoodDirector {
	
	List<StrugglingActorCallback> strugglingActorCallbacks = new ArrayList<StrugglingActorCallback>();
	
	public void registerActorCallbacks(StrugglingActorCallback callback){
		strugglingActorCallbacks.add(callback);
	}

	public void callStruglingActors(){
		for (StrugglingActorCallback callback : strugglingActorCallbacks) {
			callback.callbackMethodFromDirector();
		}
	}	
}

2.) class StrugglingActorCallback : Hollywood director uses this interface; which lays down the method used by the Director to call actors.

public interface StrugglingActorCallback {   
	void callbackMethodFromDirector();	
}

3. class SRK : SRK class which creates a callback and registers with
hollywood director. If SRK wants to apply to Hollywood director he has to register his callback which implements StrugglingActorCallback.

public class SRK {

	public void applyForHollyWoodMovie(HollywoodDirector hollywoodDirector){
		hollywoodDirector.registerActorCallbacks(new StrugglingActorCallback() {
			
			@Override
			public void callbackMethodFromDirector() {
				System.out.println("Shahrukh to be called at number at +91-9483948234");
			}
		});
	}

}

4. Now lets test this.

public class SRKTest {

	@Test
	public void test() {
		SRK srk = new SRK();
		HollywoodDirector director = new HollywoodDirector();
		srk.applyForHollyWoodMovie(director);
		//now this is directors choice when to call the struglling actors.
		director.callStruglingActors();
	}

}

I hope I am able to explain the concept of callbacks in java by this simple example.

Practical Use Case
GWT uses this approach, Async Callbacks are added to the listener events of the widgets.
When an event is triggered, like a Button is pressed, these Async Callbacks are used to perform some actions, like fetch data from server.
for example :

.....
 Button b = new Button("getData", new ClickHandler() {
      public void onClick(ClickEvent event) {
              myService.getData(input1,input2,new AsyncCallback<DataModel[]>() {
              public void onFailure(Throwable caught) {
               //  Do something with errors.
             }

             public void onSuccess(DataModel[] result) {
                updateData(result);
             }
       };
    }
});

If you want to learn more about how GWT makes Async RPC calls refer this link https://developers.google.com/web-toolkit/doc/latest/tutorial/RPC

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s