java events,handlers and listeners question

前端 未结 2 765
你的背包
你的背包 2020-12-17 04:07

Edit: I was actually able to get this to work and form, in my oppinion, a good example. The accepted answer is my example and feel free to leave comments if you

相关标签:
2条回答
  • 2020-12-17 04:31

    You're very new to Java - I think you're overcomplicating this and trying to swallow too much at once.

    Forget about your grand design. Forget about UI. Forget about Android.

    Start with a toy problem and build up.

    Can you make a single Listener class that responds to an Event and a producer class to send that Event out? Can you see the action being taken? If you can't do that, you won't get far. When you have one working, move onto the next and see if you can get your design working with these simpler objects. Only when the whole thing is working should you worry about tying in the Android and UI elements.

    Start with simple POJO models and you'll get further faster.

    0 讨论(0)
  • 2020-12-17 04:38

    I have got this working and it seems to be a great example. I will post the code below, hopefully it will help someone some day! =D PLEASE if you see that i have done something wrong, or know a better way, post some comments so i can correct it. Thanks

    Basically every event you want to create needs an Event Object, Listener Interface, AddListener Function, RemoveListener Function, FireEvent Function and the Listeners. All of this can be seen in the examples below. If you have any questions, please post in the comments and if this helps you, feel free to upvote! =P

    /* SmartApp.java */
    public class SmartApp extends Activity 
    {
        private ConnectDevice cD = new ConnectDevice();
        private DataRobot dR = new DataRobot();
        private DataBuilder dB = new DataBuilder();
        private DataSender dS = new DataSender();
        public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.intro);
    
        cD.addDataReceivedListener(new DataReceivedListener() {
            @Override
            public void dataReceivedReceived(DataReceivedEvent event) {
                // TODO Auto-generated method stub
                dR.analyzeData(event.getData());
            }
        });
        dR.addDataAnalyzedListener(new DataAnalyzedListener() {
            @Override
            public void dataAnalyzedReceived(DataAnalyzedEvent event) {
                // TODO Auto-generated method stub
                dB.submitData(event.getData());
            }
        });
        dB.addDataBuilderListener(new DataBuilderListener() {
            @Override
            public void dataBuilderReceived(DataBuilderEvent event) {
                // TODO Auto-generated method stub
                dS.sendData(event.getData());
            }
        });
          }
    }  
    

    /* ConnectDevice.java
     * This class is implementing runnable because i have a thread running that is checking
     * the contents of a socket. Irrelevant to events. */
    public class ConnectDevice implements Runnable {
    
        private List _listeners = new ArrayList();
        private String data;
    
        /* Constructor */
        public ConnectDevice() {// does some socket stuff here, irrelevant to the events}
        public void run() {// does some socket stuff here, irrelevant to the events}
    
        public synchronized void addDataReceivedListener(DataReceivedListener listener) {
            _listeners.add(listener);
        }
        public synchronized void removeDataReceivedListener(DataReceivedListener listener) {
            _listeners.remove(listener);
        }
        private synchronized void fireDataReceivedEvent(String temp) {
            DataReceivedEvent dRE = new DataReceivedEvent(this, temp);
            Iterator listeners = _listeners.iterator();
            while(listeners.hasNext()) {
                ((DataReceivedListener)listeners.next()).dataReceivedReceived(dRE);
            }
        }
        public interface DataReceivedListener {
            public void dataReceivedReceived(DataReceivedEvent event);
        }
    }  
    

    /* DataRobot.java */
    public class DataRobot {
        /* This class is for analyzing the data */
        private List _listeners = new ArrayList();
        private String data;
        public boolean analyzeData(String temp) {
            /* Analyze the data
             * This function analyzes the data, as explained in the OP
             * This function fires the analyzed data event when finished
                 * analyzing the data.
             */
            data = temp;
            fireDataAnalyzedEvent(data); // this fires the dataanalyzedevent
            return true; //for now this will always return true
        }
    
        public synchronized void addDataAnalyzedListener(DataAnalyzedListener listener) {
            _listeners.add(listener);
        }
        public synchronized void removeDataAnalyzedListener(DataAnalyzedListener listener) {
            _listeners.remove(listener);
        }
        private synchronized void fireDataAnalyzedEvent(String temp) {
            DataAnalyzedEvent dRE = new DataAnalyzedEvent(this, temp);
            Iterator listeners = _listeners.iterator();
            while(listeners.hasNext()) {
                ((DataAnalyzedListener)listeners.next()).dataAnalyzedReceived(dRE);
            }
        }
        public interface DataAnalyzedListener {
            public void dataAnalyzedReceived(DataAnalyzedEvent event);
        }
    }  
    

    /* DataBuilder.java */
    public class DataBuilder {
        private List _listeners = new ArrayList();
        private String data;
        public boolean submitData(String temp) {
                /* Builds the data
                 * This function builds the data, as explained in the OP
                 * This function fires the databuilder data event when finished
                         * building the data.
                 */
            data = temp;
            fireDataBuilderEvent(data); //firing the databuilder event when finished
            return true;
        }
        public synchronized void addDataBuilderListener(DataBuilderListener listener) {
            _listeners.add(listener);
        }
        public synchronized void removeDataBuilderListener(DataBuilderListener listener) {
            _listeners.remove(listener);
        }
        private synchronized void fireDataBuilderEvent(String temp) {
            DataBuilderEvent dRE = new DataBuilderEvent(this, temp);
            Iterator listeners = _listeners.iterator();
            while(listeners.hasNext()) {
                ((DataBuilderListener)listeners.next()).dataBuilderReceived(dRE);
            }
        }
        public interface DataBuilderListener {
            public void dataBuilderReceived(DataBuilderEvent event);
        }
    }  
    

    /* DataSender.java */
    /* this class has no event, because it is done firing events at this point */
    public class DataSender {
        private String data;
        public boolean sendData(String temp) {
            data = temp;
            return true;
        }
    }  
    

    Below here are the event objects for each event. I Have each of this defined in a separate file, not sure if that is good procedure or not.

    /* DataReceivedEvent.java */
    public class DataReceivedEvent extends EventObject{
        private String data;
        public DataReceivedEvent(Object source, String temp) {
            super(source);
            // TODO Auto-generated constructor stub
            data = temp;
        }
        public String getData() {
                // this function is just an accessor function
            return data;
        }
    }  
    

    /* DataAnalyzedEvent.java */
    public class DataAnalyzedEvent extends EventObject{
        private String data;
        public DataAnalyzedEvent(Object source, String temp) {
            super(source);
            // TODO Auto-generated constructor stub
            data = temp;
        }
        public String getData() {
                // this function is just an accessor function
            return data;
        }
    }  
    

    /* DataBuilderEvent.java */
    public class DataBuilderEvent extends EventObject {
        private String data;
        public DataBuilderEvent(Object source, String temp) {
            super(source);
            // TODO Auto-generated constructor stub
            data = temp;
        }
        public String getData() {
                // this function is just an accessor function
            return data;
        }
    }
    
    0 讨论(0)
提交回复
热议问题