java的事件处理程序和听众的问题 [英] java events,handlers and listeners question

查看:134
本文介绍了java的事件处理程序和听众的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:实际上,我是能够得到这个工作与形式,在我看来,一个很好的例子。接受的答案是我的榜样,并随时发表评论,如果您需要帮助,或者有意见。

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 need assistance or have advice.

我很新的Java和刚刚推出的活动,处理程序和听众。我发现3很好的参考线,但是,我仍然没有看到我怎么能实现这个解决我的问题。 (另外,这是正在对Android开发的。我怀疑这会改变的例子,但认为我会让你知道)

I am very new to java and have just been introduced to events, handlers and listeners. I have found 3 good references online, however, i am still not seeing how i can implement this to solve my problem. (Also, this is being developed on the android. I doubt that will change the example, but thought i would let you know)

链接到网站,我发现有帮助的:
幸福的对象
例如车厂
Java世界

Links to sites i found helpful:
happy object
example depot
java world

下面是我需要的,用任意名称和实例,而不是plopping我几百code线在这里:

Here is what i need, using arbitrary names and examples instead of plopping my hundreds of lines of code in here:

下面是我的类及其用途:
MainUI - 这会在UI
连接 - 这个类进行到插座的连接,并开始接收数据
DataRobot - 这个类进行上收,并决定如何处理它
的数据计算 DataBuilder - 这个类从datarobot采集数据,一旦达到一定量就发送一个数据块到远程数据库
DataSender - 这个类发送一个被发送给它的databuilder数据

Here are my class and their uses:
MainUI - this creates the UI
Connect - this class performs a connection to a socket and starts receiving data
DataRobot - this class performs calculations on the data that is received and decides what to do with it
DataBuilder - this class gathers the data from the datarobot and once a certain amount is reached it sends that chunk to a remote database
DataSender - this class sends that data that is sent to it by the databuilder

我跟线程执行本... =(
我认识的人建议我用事件。他给了我一个很好的解释,但是,它是在30分钟内,我不看我怎么能实现它没有看到一些更好的例子(他也从一个C#背景而言)。我想知道是否有人在那里有一些Java中的丰富经验可以使用上面我所,并告诉我什么,我需要做的。

I was implementing this with threads... =(
Someone i know suggested i use events. He gave me a great explanation, however, it was within 30 minutes and i do not see how i can implement it without seeing some better examples (he was also speaking from a C# background). I was wondering if someone out there with some great experience in java could use what i have above and show me what i need to do.

下面是我已经计划好了的事件:
dataReceived - 在连接,该事件触发该事件发生时,连接接收数据时,此事件触发它把数据发送到数据处理程序的机器人()函数
dataAnalyzed - 在datarobot这种事件发生,此事件在datarobot做处理被赋予给它的数据,当这个事件触发它发送数据到databuilder
dataBuilder - 在databuilder这种事件发生,当databuilder已经获得了一定量,并准备发送数据时,此事件触发它发送数据到datasender此事件触发。

Here are the events that i have planned out:
dataReceived - this event happens in connect, this event fires when connect receives data when this event fires it sends the data to the data robots handler() function.
dataAnalyzed - this event happens in datarobot, this event fires when datarobot is done handling the data that was given to it, when this event fires it sends the data to the databuilder
dataBuilder - this event happens in databuilder, this event fires when databuilder has received a certain amount and is ready to send the data, when this event fires it sends the data to the datasender.

我要寻找一个例子这里或至少应当做什么的讨论。其中一个我发现的例子是happyfaceobject为例,它是pretty的好。但是,我仍然有我的设计实现它的麻烦,因为我很新的Java和事件。请时,你可以帮助我,这将是极大的AP preciated。这个例子也将有助于社会作为一个整体是有欠缺的巨大的java事件例子。

I am looking for an example here or at least a discussion on what should be done. One of the examples i found was the "happyfaceobject" example and it was pretty good. However, i am still having trouble implementing it in my design, since i am VERY new to java and events. Please assist me when you can and it will be greatly appreciated. This example would also help the community as a whole as there is a lacking of great java events examples.

在此先感谢,如果您需要更多的信息让我知道。

Thanks in advance, if you need more info let me know.

推荐答案

我有这个工作,这似乎是一个很好的例子。我将张贴code以下,希望这将帮助别人有一天! = D请,如果你看到我做错了,或知道一个更好的方式,发表一些意见,这样我就可以纠正。谢谢

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

基本上每一个事件要创建需要的事件对象 Listener接口 的addListener功能 的removeListener功能 FireEvent功能监听器。所有这一切都可以看出,在以下的实施例。如果您有任何疑问,请在评论中,如果这可以帮助你,随意upvote! = P

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;
    }
}

这篇关于java的事件处理程序和听众的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆