0

I have an interface Event and a class Agent.

I want to be able to write something like agent.handle(event).

However, the classes that implement the event interface will all have different fields and things that the agent.handle method would need to know about, and so I'm struggling between either:

  1. A switch on event type which then calls the relevant handler method in Agent and likely casts the event (which is basically like a chain of InstanceOfs which I know is a sign of bad design generally).

  2. Switching to something like event.applyTo(Agent), but it feels strange having an event 'happen to' an agent, as now the event (which should really just be a description of an event and not be coupled to agents specifically) would need to contain the handling logic.

Is there a design pattern for this kind of thing that I'm missing?

It seems like this kind of thing would be pretty common in event based systems where you want to decouple events from handlers when reading messages from a single input queue.

I've played about with how the Adapter pattern would look, and my understanding is that I'd have something like EventAdapter.handle(agent, event) but I basically don't like that solution.

I've came across this solution as well which I quite like, but I'm not sure if it's too verbose.

This question on SO also seems to be a similar problem, but the solutions are either stringly typed, and/or won't catch a missing handler for an event until runtime. And this one here on SE also suffers from the same problem.

4
  • 1
    I do not understand the question or what you are trying to do. Events are typically subscribed to, not called actively. Some context may help.CommentedNov 9, 2019 at 21:50
  • 1
    I suspect that what you're looking to know is that an event-argument object can be abstract, such that a single object of a uniform type can effectively pass different sorts of information, containing the different types of parameters you're asking about. But, a more concrete example would make it easier to discuss the problem.
    – Nat
    CommentedNov 9, 2019 at 22:00
  • sorry my poor wording, but @Nat you hit the nail on the head!CommentedNov 10, 2019 at 13:04
  • @MartinMaat thanks for the feedback! I've linked another question from SO that I think I would say is closer to what I'm dealing with, and might help describe my problem better. Let me know if it does :)CommentedNov 10, 2019 at 13:15

1 Answer 1

1

First of all, what you are asking is unclear based on how you phrase the question, so if you could provide some more information about what you are trying to achieve, instead of an example of where you stuck trying to do things the way you did, it might be possible to provide better assistance.

However

Looking at it from a broader perspective, your problem appears to be a different one, not specifically related to events, agents or any of your domain details. Correct me if I am wrong, but it looks like you would like to perform different things depending on the concrete type of not only one but of two objects (i.e. specific Agents handle specific Events) but you want to keep the flexibility of interfaces/base types.

In that case, what you would need is double dispatch, which is not built into the java language, so what you should probably look into is the Visitor pattern.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.