Search code examples
javaandroiddesign-patternslistenerobserver-pattern

Is that a right way of using interface callback?


I read this and this and found out that in class B I need to save a reference to class A and when something happens in class B we execute a method defined by an interface that class A implements. Well somehow I understood it. I use interfaces in a bit different way to call a callback:

interface IHelper {
    void onActionDone ();
    void onActionFailed ();
}            

public class Helper implements IHelper {

    public Helper (Param param) {
        // here we do what Helper class intended to do
        // ...
        // now call the any of callbacks
        if(everything == OK) {
            onActionDone();
        } else {
            onActionFailed();
        }
    }

    @Override
    public void onActionDone() {}

    @Override
    public void onActionFailed() {}
}

public class MainClass () {
    new Helper(message) {
        public void onActionDone () {
            // here we can do anything we want after Helper will done its functions
        }
        public void onActionFailed () {
            // or not done
        }
    }
}

I find this way is more readable and easier to understand but not sure if this is a bad practice or not. So may I feel free to go this way further?


Solution

  • So may i feel free to go this way further?

    This way isn't quite correct.

    What you're talking about is called Observer or Subscriber/Publisher pattern.

    In simple words: a subscriber wants to receive events (magazine issues) from a publisher, so he informs (subscribe) the publisher about it. After that the publisher notifies the subscriber on an event occurred.

    In your code snippet, the publisher is Helper and the subscriber is MainClass. The publisher has a form of subscription IHelper:

    public class Helper {
        IHelper mSubscriber;
        ...
        void setSubscriber(IHelper subscriber) {
            this.mSubscriber = subscriber;
        }
        ...
    }
    

    The subscriber should fill in the form, i.e. implements IHelper, and notify the publisher about itself:

    public class MainClass implements IHelper {
        Helper mPublisher;
        ...
        void someMethod() {
            mPublisher.setSubscriber(this);
        }
        ...
    }
    

    Now, when a new magazine issue is published by the publisher, the subscriber is being notified about that:

    public class Helper {
        ...
        void newMagazineIssued() {
            mSubscriber.onActionDone();
        }
    
        void newMagazineFailed() {
            mSubscriber.onActionFailed();
        }
        ...
    }
    

    A listener example:

    If the above said is a bit confusing to you, consider a Button you have just initialized in an Activity. The button acts as a publisher, whereas the Activity acts as a subscriber. The Activity wants to be notified when the button is clicked by a user (a new magazine issued), so it subscribes to the event with setOnClickListener() where the View.OnClickListener parameter passed to the method is the subscription form. The Activity (subscriber) fills in the form, by implementing the interface and overriding the onClick() method, and pass the form to the method (subscribe). When a click occurs, the Activity is being notified about that.