Event Design Template

I am trying to understand the most appropriate (Java) design pattern that will be used to process a series of messages. Each message contains a “type” that defines how to process the data contained in the message.

I look at the Command pattern, but try to understand the roles / relevance of certain classes of commands. So far, I have decided that the recipient will contain code that implements message processing methods. Specific commands will be created based on the type of message. However, I do not know how the actual data should be transmitted. Should it be passed to the constructor of the receiver using the appropriate receiver methods called by the method of executing a specific command? Maybe these messages should be transmitted in calls to the recipient's action method?

I am completely new to this, so any guidance would be appreciated.

This can 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; } } 
+6
source share
3 answers

Ok, your headline is talking about a template for handling events. If you are talking about the real structure of events, then the Observer / Observable picture comes to mind. This will work when you want to fire an event of some type, and then the event handlers take away the event handling.

It looks like your problem is with the details of the implementation of the command template. Can you post code that shows where you are stuck?

Please note that the templates are not mutually exclusive; you can use the command template in the context of the Observable template.

EDIT - based on your code you should

1) make the statics CommandFactory .
2) pass the type to the getCommand method, which must also be static.
3) You do not need to reflect this, you can just do

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

I am not saying that you cannot use reflection, I am saying that it is too complicated what you are trying to do. In addition, they, as you do, bind a string representing the type of message with the details of the implementation of the class names of commands, which seems unnecessary.

+3
source

You are on the right track. The team template is the right solution for the intended problem.

To answer your question, you would program CommandFactory the appropriate instance of the command based on the data differentiator (in this case, some data in your message). Then you call the method on the Command instance, passing the message. A common (best) practice is to call this Execute (...) method, but you can call it whatever you want.

+1
source

You might want to take a look at the Jakarta Digester project (for XML processing), it has a SAX implementation, which is an event-based API, as described here http://www.saxproject.org/event.html , this is a short explanation, but can serve as a starting point for you.

0
source

Source: https://habr.com/ru/post/891023/


All Articles