Tworzenie niestandardowego zdarzenia w Javie

Chcę zrobić coś takiego w Javie ale nie znam drogi:

Gdy wydarzy się Zdarzenie "object 1 say 'hello'", następnie obiekt 2 odpowiada na to zdarzenie, mówiąc "cześć".

Może mi ktoś podać podpowiedź lub przykładowy kod?

Author: JDJ, 2011-06-07

3 answers

Prawdopodobnie chcesz przyjrzeć się wzorcowi obserwatora .

Oto przykładowy kod na początek:

import java.util.*;

// An interface to be implemented by everyone interested in "Hello" events
interface HelloListener {
    void someoneSaidHello();
}

// Someone who says "Hello"
class Initiater {
    private List<HelloListener> listeners = new ArrayList<HelloListener>();

    public void addListener(HelloListener toAdd) {
        listeners.add(toAdd);
    }

    public void sayHello() {
        System.out.println("Hello!!");

        // Notify everybody that may be interested.
        for (HelloListener hl : listeners)
            hl.someoneSaidHello();
    }
}

// Someone interested in "Hello" events
class Responder implements HelloListener {
    @Override
    public void someoneSaidHello() {
        System.out.println("Hello there...");
    }
}

class Test {
    public static void main(String[] args) {
        Initiater initiater = new Initiater();
        Responder responder = new Responder();

        initiater.addListener(responder);

        initiater.sayHello();  // Prints "Hello!!!" and "Hello there..."
    }
}

Pokrewny Artykuł: Java: Tworzenie niestandardowego zdarzenia

 379
Author: aioobe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-03 15:58:20

To, czego chcesz, to implementacja wzorca obserwatora . Możesz to zrobić samodzielnie lub użyć klas java, takich jak java.util.Observer i java.util.Observable

 25
Author: Bozho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-07 18:51:45

Istnieją 3 różne sposoby, aby to skonfigurować:

  1. Thrower wewnątrz Catcher
  2. Catcher wewnątrz Thrower
  3. Thrower i Catcher wewnątrz innej klasy w tym przykładzie Test

Działający przykład Githuba, który przytaczam domyślnie opcja 3, aby spróbować innych, po prostu odkomentuj blok kodu "Optional " klasy, którą chcesz być główną, i ustaw tę klasę jako zmienną ${Main-Class} w pliku build.xml:

4 Rzeczy potrzebne na stronie:

import java.util.*;//import of java.util.event

//Declaration of the event's interface type, OR import of the interface,
//OR declared somewhere else in the package
interface ThrowListener {
    public void Catch();
}
/*_____________________________________________________________*/class Thrower {
//list of catchers & corresponding function to add/remove them in the list
    List<ThrowListener> listeners = new ArrayList<ThrowListener>();
    public void addThrowListener(ThrowListener toAdd){ listeners.add(toAdd); }
    //Set of functions that Throw Events.
        public void Throw(){ for (ThrowListener hl : listeners) hl.Catch();
            System.out.println("Something thrown");
        }
////Optional: 2 things to send events to a class that is a member of the current class
. . . go to github link to see this code . . .
}

2 rzeczy potrzebne w pliku klasy do odbierania zdarzeń z klasy

/*_______________________________________________________________*/class Catcher
implements ThrowListener {//implement added to class
//Set of @Override functions that Catch Events
    @Override public void Catch() {
        System.out.println("I caught something!!");
    }
////Optional: 2 things to receive events from a class that is a member of the current class
. . . go to github link to see this code . . .
}
 20
Author: GlassGhost,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-06 20:13:50