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

  • Home
  • SEARCH
  • 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 6170033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:57:43+00:00 2026-05-23T22:57:43+00:00

I’m sure this is a basic OOP question — I’m designing a message-passing system

  • 0

I’m sure this is a basic OOP question — I’m designing a message-passing system where there are several entirely distinct message formats, but I want them all to be able to be placed on a PriorityBlockingQueue. My first thought was to define an abstract class Message and then define subclasses extending Message for each of the message types. But this means that, at the receiving end, the message processor needs to identify the subclass in order to know how to process the contents of the message. And the only way I know of doing this is with .instanceof() or Class. and it somehow doesn’t seem right.

As Scott Meyers writes,

Anytime you find yourself writing code of the form “if the object is
of type T1, then do something, but if it’s of type T2, then do
something else,” slap yourself.

(He goes on to make the point that, in polymorphism, you should have the same method names having different implementations for each subclass. I don’t see how I can make that idea work in my case – the message types themselves are utterly unrelated.)

For the sake of discussion, here are my message types:

  • ConsoleMessage, identifying a ConsoleObject and an ObjectState.
  • CardReaderRequestMessage, not containing anything but simply requesting “next card”
  • CardReaderMessage, containing a byte[80] card image and a Last Card indicator
  • CardPunchMessage, containing a byte[80] card image
  • CardPunchResponseMessage, not containing anything but signifying that the card image was copied to the punch buffer

I believe I have to know what kind of message I’m dealing with, so I suspect I should not be using polymorphic Messages. How should I design this properly?

===== EDIT to raise a follow-up question =====

I was trying to find a way to use a polymorphic Message without having, at some point, to identify its subclass. The suggested approach was to override a process() method in each subclass. Here’s my (simplified) abstract Message and two subclasses:

public abstract class Message {

    public abstract void process() {
        // subclasses of Message implement this
    }

    public static class ConsoleMessage extends Message {
        private int obj;
        private int state;
        public ConsoleMessage(int x, int y) {
            obj = x;
            state = y;
        }
        @Override
        public void process() {
            // do something with obj and state?
        }

    public static class CardReaderMessage extends Message {
        private byte[] card;
        private boolean lastCardIndicator;
        public CardReaderMessage(byte[] c, boolean lc) {
            card = c;
            lastCardIndicator = lc;
        }
        @Override
        public void process() {
            // do something with card and lastCardIndicator
        }
}

There is one queue per thread for all “inbound” Messages. Suppose my thread needs to wait for a message from the console to “resume”, but meanwhile should receive and process other message types:

waitForResumeMessage() {
    while (true) { // the following will block until a msg arrives
        Message msg = inboundMessageQueue.receiveMessage();
        msg.process();    

But now what? Some implementation of process() has moved some data somewhere, but ultimately I need to be able to write:

        if // msg was ConsoleMessage "resume" command
            return;  // .. from waitForResumeMessage()
    } // else iterate until another message
}

Which basically means finding out what class ‘msg’ belongs to.

Am I approaching this all wrong? I realize that “waiting” is not really appropriate in an “event-driven” model, but this is a long-running background worker. Perhaps the idea of using process() is more useful for changing the state of an FSM that’s guiding an event-driven thread?

  • 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-23T22:57:44+00:00Added an answer on May 23, 2026 at 10:57 pm

    Your idea is good, you should indeed start with either an abstract class or an interface called Message, and this interface should be something like this:

         public interface Message {
         void process();
         //some other methods
    }
    
    public class MessageType1  implements Message {
        @Override
        public void process() {
          //My special way to process this message
    }
    }
    

    This way the receiving end only has to receive something that IS A Message i.e. implements your Message interface, and it doesn’t really care what type of particular message all it cares is that it responds to process, so you can just implement how each particular message should be processed there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a JSP page retrieving data and when single or double quotes are
I've got a string that has curly quotes in it. I'd like to replace

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.