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 8125755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:55:06+00:00 2026-06-06T06:55:06+00:00

I have a specialized object class that sends messages to its components so that

  • 0

I have a specialized object class that sends messages to its components so that they can change themselves as needed. The object class allows me to mix and match components so that I can have different objects with similar functionality without having a huge inheritance tree. (I think this is called the component pattern.) Here’s what I want to do:

//MyObject.java
public class MyObject {

    private HashSet<MyComponent> components_;

    void send(Message message) {
        for (MyComponent component : components_) {
            component.receive(message);
        }
    }

    // other unrelated methods and variables...

}

// MyComponent.java
public interface MyComponent {

    public void receive(Message message);

    // other unrelated methods...

}

// RendererComponent.java
public class RendererComponent implements MyComponent {

    public void receive(Message message) {
        if (message.getType() == Message.Type.POSITION_CHANGED) {
            Point positionDifference = message.getInfo();
            redraw(positionDifference);
        } else if (message.getType() == Message.Type.SCORE_CHANGED) {
            Integer score = message.getInfo();
            redraw(score);
        } // else if...
    }

    private void redraw(Point positionDifference) {
        // draws item at new position...
    }

    private void redraw(Integer score) {
        // draws the new score...
    }

    // other unrelated methods and variables..
}

I have a lot of components and they receive many different types of messages. How do I write my Message class so that I can get information of different class types from it? Do I need to change the way my component class handles messages?

  • 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-06-06T06:55:07+00:00Added an answer on June 6, 2026 at 6:55 am

    I have a specialized object class that sends messages to its components so that they can change themselves as needed.

    The normal (and easy) way to do this kind of thing is with Java’s internal event publishing mechanisms. You define a class that represents an event, and an interface that event listeners / consumers must implement with an “event occurred” callback method. An event producer has a register method for registering an event consumer, and keeps a list of registered consumers. When the producer wants to send an event, it creates an event object and then calls the “event occurred” callback for all registered consumers.

    This approach is used throughout the Java’s GUI libraries, and in other places. It is lightweight and efficient … and probably a better idea than doing this kind of thing by message passing.


    A good place to start learning about events is the Introduction to Event Listeners in the Swing tutorial.

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

Sidebar

Related Questions

I have set up a UDP client/server model that can send string messages to
Java design question. I have an object that needs to maintain sets of say
Base class MessageHandler has derived classes. They would like to pass messages to each
I have the following object: [Serializable] public class ExampleImage { public int ID {
I am developing in python a file class that can read and write a
I try to have a partially specialized template class inheriting from another template class.
I often design system where I have a class in my system that has
I have a graph object, that I need to serialize. Nomatter which members i
I have A WCF service that has a class that inherits System.Web.Security.RoleProvider. In this
I have a container object that is templatized. I am trying to make a

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.