1

It is basic problem. I have class A which gives some task to class B. When class B finish the tast it must notify class A. I want register A class method as callback in class B.

I really want do it in this way, not by observer pattern with interface Observable.

public class A
{
    public void A()
    { 
        B b = new B()
        b.registerCallback(callback);
    }

    private void callback()
    {

    }

}

public class B
{
    private ???? callbackoNotify;

    public class registerCallback(??? callback)
    {
        callbackoNotify = callback;
    }

    public void notify()
    { 
        callback();
    }
}

2 Answers 2

4

You can define an interface for the callback.

interface Callback{
    void call();
}

Then, let class A implement it.

class A implements Callback{
    private B b;

    public A(){
        b = new B();
        b.registerCallback(this);
    }    

    // Implementation of the callback interface
    public void call(){

    }
}

Then, let class B to handle the callback.

public class B
{
    private Callback callbackoNotify;

    public class registerCallback(Callback callback)
    {
        callbackoNotify = callback;
    }

    public void notify()
    { 
        callbackNotify.call();
    }
}

But in the above scenario, callbackNotify can be null. Therefore, it is better if you can pass that callback in the constructor to B.

Hope you got the idea.

Sign up to request clarification or add additional context in comments.

3 Comments

Then class A Has public method 'call' which everyone can call, although only B should can.
So, make A an abstract class that extends Callback. Make B a concrete class that extends A with a protected method that is "call". Maybe if you're having these issues in the first place, you should look into an asynchronous framework such as Ratpack.
@aQuu That is the use of interfaces. In OOP, if we are to call this call method, we should have an object of type Callback. If you are doing a.call() where a is of type A, then it is a bad practice. Even in class B, we keep the reference to the callback in the type Callback
3

You can do this without callbacks as well. Here's an example:

A a = ...
B b = ...

Let's say b has a doTask method like this:

public void doTask(Runnable task);

A can now call it like this:

b.doTask(() -> {System.out.println("Hi There");});

However, A will not be informed when the task gets completed. You could simply change the task like this:

Runnable taskForB = () -> {System.out.println("Hi There");};
Runnable wrapperForTaskWithCallback = () -> {
    taskForB.run();
    taskWasFinished();
};

And then run the wrapper task instead:

b.doTask(wrapperForTaskWithCallback);

And give A a method:

public void taskWasFinished();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.