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){

	public void callStruglingActors(){
		for (StrugglingActorCallback callback : strugglingActorCallbacks) {

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() {
			public void callbackMethodFromDirector() {
				System.out.println("Shahrukh to be called at number at +91-9483948234");


4. Now lets test this.

public class SRKTest {

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


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) {

If you want to learn more about how GWT makes Async RPC calls refer this link


Leave a Reply

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

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.