Skip to content

Subscribers

Definition

To define a subscriber, you have multiple options.

Extend the Subscriber class:

public class ExampleSubscriber extends Subscriber {
    // ...
}

Implement the ISubscriber interface:

public class ExampleSubscriber implements ISubscriber {
    // ...
}

Subscription

Once you've defined your subscriber, you can subscribe it to an event bus in a variety of ways.

Inside the subscriber itself.

public class Consts {
    private static final IEventBus EVENT_BUS = new EventBus();
}

public class ExampleSubscriber extends Subscriber {

    public ExampleSubscriber() {
        Consts.EVENT_BUS.subscribe(this);
    }
}
public class Consts {
    private static final IEventBus EVENT_BUS = new EventBus();
}

public class ExampleSubscriber extends Subscriber {

    public ExampleSubscriber() {

    }

    public void subscribe() {
        Consts.EVENT_BUS.subscribe(this);
    }
}

Or externally.

public class Example {

    private static final IEventBus EVENT_BUS = new EventBus();

    public void handleSubscription() {
        EVENT_BUS.subscribe(new ExampleSubscriber());
    }
}
public class Example {

    private static final IEventBus EVENT_BUS = new EventBus();
    private final ExampleSubscriber subscriber = new ExampleSubscriber();

    public void handleSubscription() {
        EVENT_BUS.subscribe(subscriber);
    }
}