My Java program will be receiving messages with a predefined structure. Each message has multiple fields, and the next field may have certain values (and should be processed in certain way) depending on the current field value:
For example:
1. FIELD1-FIELD2-CUSTOMDATA-OTHERDATA
2. FIELD1-FIELD2-FIELD3-CUSTOMDATA-OTHERDATA
Here, depending on the type of FIELD1, certain decoding logic should run next; So certain parser should be used.
Is there any design pattern for such ?
I have thought of defining a class for each message type and provide the input step by step to certain implementation. But this doesn’t sound too good for me.
Thoughts?
Thanks in advance.
Unless you need the structure to be changed dynamically, I would write a parser which uses a switch and listener to handle the messages.