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.

  • 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. – Martin Maat Nov 9 '19 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 Nov 9 '19 at 22:00
  • sorry my poor wording, but @Nat you hit the nail on the head! – Jordan Mackie Nov 10 '19 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 :) – Jordan Mackie Nov 10 '19 at 13:15
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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.