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

We have a system that has some Bash scripts running besides Java code. Since
We have a system that runs in IIS. The system should always run using
I have a system that has many components interacting with each other. It occured
I have a system that takes Samples. I have multiple client threads in the
I have a system set up currently that is using celery with a redis
I have a very simple C# project that has a UI process and a
We have system here that uses Java JNI to call a function in a
I have a system that takes dynamic data, puts it in HTML for layout
I have big system that make my system crash hard. When I boot up,
I have a system that runs like this: main.exe runs sub.exe runs sub2.exe and

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.