Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3676898
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:09:21+00:00 2026-05-19T03:09:21+00:00

I have a system that manages events that happen to multiple actors using a

  • 0

I have a system that manages events that happen to multiple actors using a simple queue-based system. Event is a simple class that has time and an abstract method fire(). Actor is a simple interface that implements two methods – think() and act(). Each actor has 2 basic events, that are implemented like that:

public class ActorThinkEvent extends Event {
    private Actor actor;

    public ActorThinkEvent(Actor actor, long time) {
        super(time);
        this.actor = actor;
    }

    public void fire() {
        Main.game.queue.add(actor.think());
    }
}
public abstract class ActorActEvent extends Event {
    protected Actor actor;

    protected ActorActEvent(Actor actor, long time) {
        super(time);
        this.actor = actor;
    }

    public void fire() {
        long spent = act();
        Main.game.queue.add(new ActorThinkEvent(actor, time + spent));
    }

    public abstract long act();
}

This way, “thinking” invocation is fixed, it is supposed to generate an ActorActEvent that would be added to the global queue, while “acting” invocation is done using a custom ActorActEvent-based class that implements the action, just returning the time spent, and new “think” event would be added to the queue automatically.

The problem I see here is that implementation of ActorActEvent-based classes comes down to simple delegation from ActorActEvent.act() to some method of Actor class with pre-stored arguments, i.e. all my ActEvent classes look very similar:

public class ActorMoveEvent extends ActorActEvent {
    private Coords delta;

    public ActorMoveEvent(Actor actor, long time, Coords delta) {
        super(actor, time);
        this.delta = delta;
    }

    public long act() {
        return actor.moveBy(delta);
    }
}
public class ActorReloadEvent extends ActorActEvent {
    public ActorReloadEvent(Actor actor, long time) {
        super(actor, time);
    }

    public long act() {
        return actor.reload();
    }
}
public class ActorPickupEvent extends ActorActEvent {
    private ItemStash wantedItems;

    public ActorPickupEvent(Actor actor, long time, ItemStash wantedItems) {
        super(actor, time);
        this.wantedItems = wantedItems;
    }

    public long act() {
        return actor.pickupItems(wantedItems);
    }
}

I might up end with several dozens of such classes. If I understand correctly, it’s classic implementation of Command pattern. However, I don’t feel good about all these delegation classes and especially about writing them all manually.

I thought of using Java’s reflection and stuff like Method.invoke(), while passing Method instance and pre-stored arguments to it in generic ActorActEvent class, however, it would be fairly slow. I thought of writing up a generator for all these classes, but it looks like a fairly clumsy solution to me.

If it would be some modern scripting/functional language, I’d end up using block/closure-like structure, preparing it first and invoking it when the time comes. Alas, I don’t know how to make it efficiently in Java.

Are there any bright ideas on what can I do to improve the situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:09:21+00:00Added an answer on May 19, 2026 at 3:09 am

    My advice is to go down the reflection path. Worrying that “it would be fairly slow” is premature in this case. The performance of reflection has been pretty decent for the past decade now, and you will be able to cache your Method objects to save the time spent reparsing names (which is probably the biggest performance hit). This approach is flexible and quick to implement.

    If you really like your closures though, you can experiment with anonymous inner classes for your events. There are a few restrictions – you will still have to declare your methods so it is a little more verbose, and you’ll need to declare your variables as final in order to access them from within the inner class. This approach will give you all the performance and safety of static types but you’ll still have to have a class for each event.

    Historically, events have exhibited a ‘worst of both worlds’ behaviour and passed around Objects, relying on the target to know how to cast and deal with them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
I want to have generalised email templates. Currently I have multiple email templates with
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have found this example on StackOverflow: var people = new List<Person> { new
I have a snippet to create a 'Like' button for our news site: <iframe
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
Let say I have the following desire, to simplify the IConvertible's to allow me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.