Skip to content

Instantly share code, notes, and snippets.

@pokk
Last active April 5, 2017 02:37
Show Gist options
  • Save pokk/e697f998069e78ef59d65fc4796ff073 to your computer and use it in GitHub Desktop.
Save pokk/e697f998069e78ef59d65fc4796ff073 to your computer and use it in GitHub Desktop.
How to implement callback method

[TOC]

Introduction

How to implement a callback method.

Callback's callbacker

public class SubClass {
    interface MyCallback {
        void callback();
    }

    MyCallback myCallback;

    void setCallbackMethod(MyCallback callback) {
        if (null != callback)
            this.myCallback = callback;
    }

    void doSomething() {
        this.myCallback.callback();
    }
}

Callback's callbackable

public class ImplementClass implement MyCallback {
    SubClass subClass;

    public static void main(String[] args) {
        this.subClass = new SubClass();
        this.subClass.setCallbackMethod(this);

        this.subClass.doSomething();
    }

    @override
    public void callback() {
        // do something.
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment