5 Replies Latest reply: Dec 3, 2013 3:08 PM by csh RSS

    Use EventListener + EventObject in non-Swing project?

    csh

      Hi,

       

      I want to use the Observer pattern in a pure Java project.

       

      I see several options:

       

      1. I use the java.util.Observer/Observable classes. However Observable is a class, not an interface, so I can't use it, if my event source extends another class.

      2. I use java.util.EventListener + EventObject.

       

      But most examples I find are always tight to Swing development, event the official Java guide.

       

      Generelly I see nothing against using java.util.EventObject, but I feel it has been designed for Swing development.

       

      I only feel it is unnecessary overhead to write your own event objects every time. Instead I could just call the listener methods, without the event object, but directly with the parameters I need.

       

      Or is this bad design? I've seen other libraries doing that as well.

       

      What do you think?

        • 1. Re: Use EventListener + EventObject in non-Swing project?
          TPD-Opitz

          Looking at your requirements, in which way is EventListerner+EventObject more suitable as Observer+EventObject?

           

          Observable is a Class.There is no point in refusing it because of this reason.

          It provides infrastructure for handling Observer objects and delivering the Events to them. With the "Swing"-approach you would have to implement that all yourself...

          You could create an Observable object in your event source as property and delegate calls to it.

           

          bye

          TPD

          • 2. Re: Use EventListener + EventObject in non-Swing project?
            csh

            I don't like the Observer/Observable pattern for two reasons:

            1. Your event source needs a reference to a Observable, if it can't extend from it.

            2. Observer only has a method which takes an Object as parameter, so you always have to cast it and don't know what you really get. I'd like to have a type safe pattern.

             

            I also read this article and I think working with addXXXListener() and removeXXXListener() is fine approach.

            I'd rather want to know, what is a good design for event listeners.

             

            Do they have to have ONE method which takes a EventObject?

            1.:

            interface MyEventListener extends EventListener

            {

            void onEvent(Event e); // Event extends java.util.EventObject

            }

             

            Or can they also take single parameters?

            2.:

            interface MyEventListener extends EventListener

            {

            void onEvent(int arg1, boolean arg2);

            }

             

            or even several methods per listener:

            3.:

            interface MyEventListener extends EventListener

            {

            void onEvent1(int arg1, boolean arg2);

            void onEvent2(int arg1, boolean arg2);

            }

             

            The latter one is used e.g. here and I wonder if this is good design. It dispatches events (different methods) for different states of a connection.

             

            Obviously all 3 approaches work. I only wonder, if there's a "best practice". All samples I found, only apply to Swing UI development.

            • 3. Re: Use EventListener + EventObject in non-Swing project?
              rp0428
              I don't like the Observer/Observable pattern for two reasons:

              Huh? Then why did you create this thread to begin with? Now you say that you don't like it?

               

              This is what you first said:

              I want to use the Observer pattern in a pure Java project.

              But now you don't like it and ask a different question instead?

              I'd rather want to know, what is a good design for event listeners.

              Why don't you start over and tell us what PROBLEM you are trying to solve. Provide some example code, if you have it, that illustrates the problem.

              All samples I found, only apply to Swing UI development.

              I didn't have any trouble finding a Java example:

              http://www.tutorialspoint.com/design_pattern/observer_pattern.htm

               


              • 4. Re: Use EventListener + EventObject in non-Swing project?
                TPD-Opitz

                csh wrote:

                I don't like the Observer/Observable pattern for two reasons:

                1. Your event source needs a reference to a Observable, if it can't extend from it.

                Actually this is a good thing because one of the OO best practices is "favour composition over inheritance".

                 

                csh wrote:

                2. Observer only has a method which takes an Object as parameter, so you always have to cast it and don't know what you really get. I'd like to have a type safe pattern.

                Typesafety is a strong argument.

                 

                csh wrote:

                 

                I also read this article and I think working with addXXXListener() and removeXXXListener() is fine approach.

                I'd rather want to know, what is a good design for event listeners.

                Which way you ever will go: there is no need to have your own Listener Interface to extend java.util.EventListener unless you want to use them in a AWT/Swing context.

                 

                csh wrote:

                 

                Do they have to have ONE method which takes a EventObject?

                [...]

                Or can they also take single parameters?

                [...]

                or even several methods per listener:

                [...]

                Obviously all 3 approaches work. I only wonder, if there's a "best practice". All samples I found, only apply to Swing UI development.

                I'd say: no, there is no "best practice". I for myself prefer option one or two. The third Option implies that the methods called are closely related to each other somehow and that a certain Listener object might be interested in more than one.

                 

                bye

                TPD

                • 5. Re: Use EventListener + EventObject in non-Swing project?
                  csh

                  @rp0428, sorry obviously I don't like the Java classes (Observer/Observable) for the reasons I mentioned (type safety especially). The pattern itself is fine of course.

                  For now I have no concrete problem to solve. I want to implement a messaging system.

                  E.g. the user should register an event listener to listen for incoming messages.

                   

                  I'd thought about something like:

                   

                  addMessageListener(new MessageListener() {

                  void onIncomingMessage(Message message)

                  {

                  }

                  }

                   

                  I only wanted to know, if this is a good approach, or if there's a best practice approach or convention, like "you always should use an EventObject as a single argument" like:

                   

                  addMessageListener(new MessageListener() {

                  void onIncomingMessage(MessageEvent e) // extends EventObject

                  {

                  // e.getMessage()

                  }

                  }

                   

                  or

                   

                  addMessageObserver(new MessageObserver() { // implements java.util.Observer

                  void update(Observable source, Object object) {

                  // Message message = (Message)object;

                  }

                  }