Moon Light Box

Time is Money

Java - Callback

| Comments

Sometimes, a class need execute some specific task when it is triggered by other class calling.
In other words, a morning call service is a callback mechanism in our life.
You tell waiter to call you at six every morning.
Then waiter will give you a morning call when time's up.

And we can use interface to achieve this concept.

public interface MorningCall {
    public void call();
}
public class Guest implements MorningCall {
    public static void main(String[] args) {
        Waiter mWaiter = new Waiter();
        mWaiter.makeCall(this);
    };

    @Override
    public void call() {
        System.println.out("Guest: I Get Up.");
    }
}
public class Waiter {
    public void makeCall(MorningCall mMorningCall) {
        System.println.out("Waiter: Make a Morning Call At Six O'Clock.");
        mMorningCall.call();
    }
}

Comments

comments powered by Disqus