Personal simple implementation of handler idea


      Sometimes in order to enable multiple people to develop smoothly according to the same protocol document, or to separate UI and business logic, handlers are used , and handlers can be used for decoupling. For example , the logic of UI control can be written by A , and then added to the handler list, waiting for the business logic to pass through the handler manager, send a corresponding tag to find it and pass parameters (callbacks), thereby indirectly running the corresponding method. You don't need to know how the other party implements it, just wait to be called and send your own results to let the handler manager call the called thing.

       In this way, you can also avoid writing various and random callbacks everywhere, which makes the architecture more and more complicated, and you can avoid the method of polling the variable through global variables and threads.

       As for my own thing, if I want to pass multiple parameters, it's better to put a List ...

       The downside is that it wastes a bit of CPU time and memory.

       Rough structure:

     


JAVA version implementation:

       structure:


package cjz.tools.handlerMsg;

 

import java.util.ArrayList;

 

public class Handler
{

    /**Message queue, which stores the message objects that the user implements through the interface and customizes the implementation**/
    private static ArrayList<HandlerInterface> handlers =
                         new ArrayList<HandlerInterface>();

   

    /**Add message interface object**/
    public static void addHandler(HandlerInterface handler){
        Handler.handlers.add(handler);
    }

   

    /**Remove message interface object**/
    public static boolean removeHandler(HandlerInterface handler)
    {
        try
        {
            Handler.handlers.remove(handler);
        }catch (Exception e) {
            return false;
        }
        return true;
    }

   

    /**Send message to marker

     * @param MsgFlag message object flag, used to identify which message objects should receive this data object**/
    public static void sendHandlerMsg(int MsgFlag,Object object)
    {
        for(HandlerInterface handler:handlers)
        {
            try
            {
                if(handler.throwMyMsgFlag() == MsgFlag)
                {
                    handler.getHandlerMsg(object);
                }  
            }catch (Exception e)
            {
                e.printStackTrace ();
            }
        }

    }

}
package cjz.tools.handlerMsg;

 

public interface HandlerInterface
{

    /**Please return your custom message flag, so that the message can be delivered correctly**/
    public int throwMyMsgFlag();

   

    /** Please implement the message processing process after you receive the message**/
    public void getHandlerMsg(Object object);

}


package cjz.tools.handlerMsgExample;

import cjz.tools.handlerMsg.Handler;

import cjz.tools.handlerMsg.HandlerInterface;

public class TestRead1

{

	private final static int myMsgFlag1 = 0x01;
	public TestRead1()
	{
		HandlerInterface handler1 = new HandlerInterface()
		{
			@Override
			public int throwMyMsgFlag()
			{
				return myMsgFlag1;
			}

			@Override
			public void getHandlerMsg(Object object)
			{
				System.out.println("handler1:" + object);
			}
		};
		Handler.addHandler(handler1);

	}

}


package cjz.tools.handlerMsgExample;

import cjz.tools.handlerMsg.Handler;
import cjz.tools.handlerMsg.HandlerInterface;

public class TestRead2 {
	private final static int myMsgFlag1 = 0x01;

	public TestRead2() {
		HandlerInterface handler1 = new HandlerInterface() {
			@Override
			public int throwMyMsgFlag() {
				return myMsgFlag1;
			}

			@Override
			public void getHandlerMsg(Object object) {
				System.out.println("handler2:" + object);
			}
		};
		Handler.addHandler(handler1);
	}
}


package cjz.tools.handlerMsgExample;

import cjz.tools.handlerMsg.Handler;

import cjz.tools.handlerMsg.HandlerInterface;

public class TestRead3
{
	private final static int myMsgFlag2 = 0x02;
	public TestRead3()
	{
		HandlerInterface handler3 = new HandlerInterface()
		{
			@Override
			public int throwMyMsgFlag()
			{
				return myMsgFlag2;
			}

			@Override
			public void getHandlerMsg(Object object)
			{
				System.out.println("handler3:" + object);
			}

		};

		Handler.addHandler(handler3);

	}

}


package cjz.tools.handlerMsgExample;

import cjz.tools.handlerMsg.Handler;

public class TestWriteThread {
	public TestWriteThread() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 256; i++) {
					Handler.sendHandlerMsg(0x01, "test handler msg:" + i);
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace ();
					}
				}
			}
		}).start();
	}

}
package cjz.tools.handlerMsgExample;

import cjz.tools.handlerMsg.Handler;

public class TestWriteThread2 {
	public TestWriteThread2() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 1024; i++) {
					Handler.sendHandlerMsg(0x02, "numer:" + i);
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace ();
					}
				}
			}
		}).start();
	}

}



running result:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325519196&siteId=291194637