How do I launch custom events in java?

1

I have been investigating on how to add my own events to classes, but I have not seen any example of more than one event, so I have investigated how the java api had done it, and this is my result:

public class EventoUno extends EventObject{
    public EventoUno(Object source){
        super(source);
    }
}
public class EventoDos extends EventObject{
    public EventoDos(Object source){
        super(source);
    }
}
public interface EventoListener extends EventListener{
    public void eventoUnoOcurrio(EventoUno e1);
    public void eventoDosOcurrio(EventoDos e2);
}
public abstract class EventoAdapter implements EventoListener{
    public void eventoUnoOcurrio(EventoUno e1){}
    public void eventoDosOcurrio(EventoDos e2){}
}
public class Clase {

    EventListenerList ell = new EventListenerList();

public void addEventoUnoListener(EventoAdapter e1){
    ell.add(EventoListener.class, e1);
}

public void addEventoDosListener(EventoAdapter e2){
    ell.add(EventoListener.class, e2);
}

public void removeEventoUnoListener(EventoAdapter e1){
    ell.remove(EventoListener.class, e1);
}

public void removeEventoDosListener(EventoAdapter e2){
    ell.remove(EventoListener.class, e2);
}

    public void producirEventoUno(){

    }

    public void producirEventoDos(){

    }
}
public class app {

    public static void main(String[] args) {
        Clase c = new Clase();
        c.addEventoUnoListener(new EventoAdapter() {
            @Override
            public void eventoUnoOcurrio(EventoUno e1) {
                System.out.println("Evento 1 ocurrio");
            }
        }
        );

        c.addEventoUnoListener(new EventoAdapter() {
            @Override
            public void eventoUnoOcurrio(EventoUno e1) {
                System.out.println("Evento 2 ocurrio");
            }
        }
        );

        c.producirEventoUno();
        c.producirEventoDos();
    }
}

The question I have is how do I skip the event when I run produceEventOne (), and produceEventDos ().

Thank you.

Greetings.

    
asked by Esloop 18.09.2017 в 10:57
source

2 answers

1
e1.eventoUnoOcurrio(new EventoUno());

There is nothing else, simply when your logic decides that it has to signal an event, it executes the appropriate method of listener , passing the information. That is what is called "notify" the event.

One important thing is that allowing a single listener is strange and very restrictive of possible uses. Keep a list of listeners , so that any component that wishes to can add yours without eliminating those that other components have added.

Related to the above, if a listener processes several events, do not add the 'listener' differently according to which event it should process, since that complicates everything. Simply make a single list of listeners ; the components that are not interested in eventoDos will simply leave the implementation empty in the listener that they register.

In fact, I generally recommend following the practice of having a single event per listener ; the only case in which it would be acceptable - if not correct - to include the process logic of several events in the same 'listener' would be in the event that the events were intimately linked (for example, events of borrarElemento and borrarTodosLosElementos from a list). And still I would prefer listeners separated.

    
answered by 18.09.2017 / 11:09
source
1

Actually it's very simple: Actually the pattern you're trying to apply is the Observable / Observer:

You have a class that launches an event and you want it to be observable. In your case, only one observer is accepted, but you could change it to keep a list of observers. Then, when you want to produce an event, you go through the list of observers and call their methods. In your case:

void producirEventoUno() {
    if (e1!=null) {
        e1.eventoUnoOcurrio();
    }
}

If you had a list of observers, in a generic case:

class MiClaseObservable {

    List<Observador> observadores= new ArrayList<>();

    void addObservador(Observador o) {
        observadores.add(o);
    }

    void lanzarEvento() {
        observadores.forEach(obs -> obs.eventoOcurrido());
    }
}

PS: To whom it may be of interest, the equivalent OS question in English is this .

    
answered by 18.09.2017 в 11:08