Studying interfaces, I came across such a thing as a callbak interface. If I understood correctly, then this is a beacon interface that can be screwed to something that will pull this interface in case of any changes (for example, such an interface can be weighed on a communication channel and in the case of incoming messages, perform some action) and it in turn will perform the prescribed methods. The question is how to implement it. The examples from the tutorials that I found on the network are not completely clear to me. Please explain how this thing is implemented, or share links to good materials on this topic.

  • one
    For java, this topic is widely covered. There is also an Observer. Not a single java book is complete without an Observer. More ancient technique and probably simpler. Without unnecessary objects, interlayers. Just as you want - the interface. And Listener assumes the transfer of a message object. - Sergey
  • I liked the idea with Observer, but Observer probably applies to large-scale projects? for example, I have a set of web socket connections. and there is one point that sends messages in the form of numbers. the numbers are collected in the collection and the average value that comes to customers on the socket is calculated. I want to somehow fix the collection in the collection so that the customers receive a modified average value of the collection as the collection changes, without refreshing the page. Does it make sense to implement in this case this pattern? - Iga
  • Observer is suitable for any project. - Sergey
  • Here, instead of a callback, you can stir up the queue. A thread that reads numbers from a dot can, instead of calling a callback, queue numbers. Another thread is waiting for the numbers to appear in this queue and send further down the sockets. - Sergey
  • It is possible, but in my case the web-app connection is made on vert.x 3. there the web-card acts both as a read stream and as a write stream (and throwing more streams there, I think it is not advisable), however I still couldn’t get it that described above means of this framework therefore came to listeners - Iga

2 answers 2

callback - callback - the concept and application is very wide.
A callback like your beacon is called listener (our listener). They are registered by the registration function and then they are called not clearly by whom and it is not known when (will the event come or not? When? Who knows ...)

There are other types of callbacks.

For example, in the Win32 API, there is a function EnumWindows for enumerating open windows. It takes as a parameter the EnumWindowsProc callback function.
EnumWindows knows that you need these windows, but do not know why. In addition, for some important reasons, she does not want to give you a ready list at once.
But with the help of callback, she will pass all one by one. For each window detected, your EnumWindowsProc function is called so that you can figure it out yourself in this window. And here the callback function, if called, is only during the operation of EnumWindows , and not sometime later.

    Offtopic to demonstrate ideas with the queue.

    Here is a slightly reworked example about BlockingQueue from Javadoc :
    One thread in an infinite loop inserts into the queue, the other retrieves. The reader of numbers is practically not blocked (only for the duration of the call queue.take() ). Observer or any other callback will make it wait until that same callback is completed before reading the next number.

     class Producer implements Runnable { private final BlockingQueue queue; Producer(BlockingQueue q) { queue = q; } public void run() { try { while (true) { queue.put(produce()); } } catch (InterruptedException ex) { ... handle ...} } Number produce() { // читает из точки очередное число // или столько сколько нужно для вычисления среднего и возвращает его Number x = ...; return x; } } class Consumer implements Runnable { private final BlockingQueue queue; Consumer(BlockingQueue q) { queue = q; } public void run() { try { while (true) { consume(queue.take()); } } catch (InterruptedException ex) { ... handle ...} } void consume(Number x) { // рассылает число по сокетам } } class Setup { void main() { BlockingQueue<Number> q = new SomeQueueImplementation(); Producer p = new Producer(q); Consumer c1 = new Consumer(q); Consumer c2 = new Consumer(q); new Thread(p).start(); new Thread(c1).start(); new Thread(c2).start(); } } 
    • one
      In its current form, this answer is not directly related to the question. It is necessary either to issue it in a separate question, or merge with the main answer as a counterexample, or pour it on pastebin and give it to the vehicle. - Nofate