Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern to process events

I am trying to understand the most suitable (Java) design pattern to use to process a series of messages. Each message includes a "type" which determines how the data contained in the message should be processed.

I have been considering the Command pattern, but are struggling to understand the roles/relevance of the specific Command classes. So far, I have determined that the receiver will contain the code that implements the message processing methods. Concrete commands would be instantiated based on message type. However, I have no idea how the actual message data should be passed. Should it be passed to the receiver constructor with the appropriate receiver methods being called by the concrete command execute method? Maybe the message data should be passed in the receiver action method invocations?

I am fairly new to all of this so any guidance would be appreciated.

This may help:

public interface Command {
    public void execute(String msg);
}

public class AO1Command implements Command {

    Receiver rec = new Receiver();

    public void execute(String msg) {
        rec.admit(msg);
    }
}

public class CommandFactory {

    public protected CommandFactory () { }

    public static Command getInstance(String type)  {

       if (type.equals("A01")) return new A01Command();
       else if (type.equals("A02")) return new A02Command();
       else {
          return null; 
       }
}
like image 659
skyman Avatar asked Jun 21 '11 15:06

skyman


People also ask

What are event-driven patterns?

The event-driven architecture pattern is a popular distributed asynchronous architecture pattern used to produce highly scalable applications. It is also highly adaptable and can be used for small applications and as well as large, complex ones.

Which is looking for patterns where one event is connected to another event?

An event pattern is one of the key concepts used in many advanced real-time analytics approaches, particularly CEP systems. It captures relationships between events in the real world.


2 Answers

Ok, your title says a pattern for handling events. If you are talking about an actual event framework, then the Observer/Observable pattern comes to mind. This would work when you want do fire an event of some type, then have event handlers pick up the processing of the events.

Seems like your problem is in the implementation details of the command pattern. Can you post some code that shows where you are stuck?

Note that patterns are not mutually exclusive, you could use the command pattern in the context of the Observable pattern.

EDIT -- based on your code, you should

1) make the CommandFactory static.
2) pass the type to the getCommand method, which should also be static.
3) You don't need reflection for this, you can simply do

if (type == "type1") return new Command1();
else if (type == "type2") return new Command2();
...

Im not saying you can't use reflection, I'm saying its overcomplicating what you are trying to do. Plus, they way you are doing it binds the the String that represents the message type to the implementation details of the command class names, which seems unnecessary.

like image 114
hvgotcodes Avatar answered Sep 28 '22 00:09

hvgotcodes


You are on the right track. A Command pattern is the appropriate solution to the outlined problem.

To answer your question, you would have your CommandFactory instantiate an appropriate Command instance based on the data differentiator (in this case some data in your message). You would then invoke a method on the Command instance, passing in your message. It is common (best) practice to call this method Execute(...), but you can call it whatever you want.

like image 36
Perception Avatar answered Sep 27 '22 22:09

Perception