Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:41:13+00:00 2026-05-29T19:41:13+00:00

My Java program will be receiving messages with a predefined structure. Each message has

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:41:15+00:00Added an answer on May 29, 2026 at 7:41 pm

    Unless you need the structure to be changed dynamically, I would write a parser which uses a switch and listener to handle the messages.

    interface MessageListener {
        void message1(String field2, Object customData, Object otherData);
        void message2(String field2, String field3, Object customData, Object otherData);
        // other message types
    }
    
    
    MessageListener ml = 
    String firstField = getField();
    switch(firstField) {
        case messageType1: {
            String field2 = getField();
            Object customData = getData();
            Object otherData = getData();
            ml.message1(field2, customData, otherData);
            break;
        }
        case messageType2: {
            String field2 = getField();
            String field3 = getField();
            Object customData = getData();
            Object otherData = getData();
            ml.message2(field2, field3, customData, otherData);
            break;
        }
        //parse other message types
    
        default:
            // report unknown message type.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to code a Java program that will receive messages from network and
As we all know Java program will start executing from the public static void
I have a java program that will work with a variety of Java Beans.
How to write a java program which will tell me whether I have internet
I'm writing a Java program that will be used on both Windows and Mac.
I am trying to create a java program that will interact with jUDDI to
I am trying to write a java program that will automatically download and name
Hi I want to write a java program where I will provide my email
Am using java to develop program that receives and processes SMS messages. I have
I'm trying to make a bash script that will talk to a java program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.