How to transfer data between multiple classes

Some time ago, I met a problem. The main thread starts a sub thread, and the sub thread starts another sub thread. After the data processing is completed, it needs to be transferred back to the main thread. At that time, the data is transferred back using layers of callbacks. The task chain is very long, and I think it's easy to have problems. Although it can be solved using the open source framework or the observer mode, I still think it's a little complex Miscellaneous. Today I think of a slightly better way to encapsulate the interface callback, and I can also transfer data between multiple classes in the future. The code is simple and can be modified at will. The code is as follows:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author:MYM
 * date:2017-1-6
 */
public class CallBackUtils {
    private static CallBackUtils callBackUtils;
    private static List<MessageCallBack> messageCallBacks = new ArrayList<>();
    private Map<Object,MessageCallBack> objects = new HashMap<>();
    private CallBackUtils() {
    }
    public static CallBackUtils getInstance(){
        if (null != callBackUtils){

        }else {//Using DCL to realize thread safety
            synchronized (CallBackUtils.class){
                if (null == callBackUtils){
                    callBackUtils = new CallBackUtils();
                }
            }
        }
        return callBackUtils;
    }
    /** Remove monitor */
    public void remove(Object o){
        if (objects.containsKey(o)){
            MessageCallBack messageCallBack = objects.get(o);
            if (messageCallBacks.contains(messageCallBack))
                messageCallBacks.remove(messageCallBack);
        }
    }

    /**
     * Send messages to all callbacks
     * @param value
     */
    public void sendMessage(String value){
        for (MessageCallBack messageCallBack : messageCallBacks){
            messageCallBack.callBack(value);
        }
    }

    /**
     * Send a message to the specified class. If the class is not found or is not bound to listen, no message will be sent
     * @param o Bind listening object
     * @param value send content
     */
    public void sendMessageSingle(Object o,String value){
        if (objects.containsKey(o)){
            MessageCallBack messageCallBack = objects.get(o);
            if (messageCallBacks.contains(messageCallBack))
                messageCallBack.callBack(value);
        }
    }

    /**
     * Set listening
     * @param o Mainly used for unbinding
     * @param messageCallBack
     */
    public void setListener(Object o,MessageCallBack messageCallBack){
        if (!messageCallBacks.contains(messageCallBack))
            messageCallBacks.add(messageCallBack);
        if (!objects.containsKey(o))
            objects.put(o,messageCallBack);
    }
    interface MessageCallBack{
        void callBack(String value);
    }
}

It's easy to use, so we won't show it here

Keywords: Java

Added by ilikemath2002 on Mon, 04 May 2020 07:03:48 +0300