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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:50:33+00:00 2026-06-08T06:50:33+00:00

In my scenario, I have Component A and Component B which communicates through a

  • 0

In my scenario, I have Component A and Component B which communicates through a Message class.

My message class looks like this

class Message {
    virtual void prepare();
    virtual void parse();
    virtual void handle();
};

Any message is a subclass of the Message class, for example:

class MessageA: public Message {
    void prepare() {
    ...
    }
    void parse() {
    ...
    }
    void handle() {
    componentA->executeFunctionABC(); // componentA is a global pointer
    }
};

Component A is compiled with MessageA

Component B is compiled with MessageA

So say when Component A wants to send a message to Component B, it will instantiate a MessageA object, prepare() it and send it out. When Component B receives the message through the socket, it will parse() it and handle() it.

My problem now lies in the handle() function. only the receiver of a message will call the handle() function. The implementation of the handle() function needs to execute certain routines which involves functions in the receiving Component.

I can now solve this by using PREPROCESSOR like this:

void handle() {
#ifdef COMPILE_FOR_COMPONENT_A
componentA->executeFunctionABC();
#endif
}

But it looks ugly. I wonder if there is any design pattern that can do this correctly?

  • 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-08T06:50:36+00:00Added an answer on June 8, 2026 at 6:50 am

    If your components implement a common interface, you can pass the component into the handle method:

    class Component {
      virtual void executeFunctionABC() = 0;
      virtual void executeFunctionDEF() = 0;
    }
    
    class MessageA : public Message {
      void handle(Component *c) {
        c->executeFunctionABC();
      }
    }
    

    When the component receives a message, it would call:

    message->handle(this);
    

    Also, from your description, prepare and parse seem to be primarily used for creating / restoring a message, so I would make them factory methods (either a static method in Message or a separate MessageFactory class) instead of virtual methods on the Message class.


    Edit: Alternatively, you can use the visitor pattern by having separate handle methods for each component:

    class MessageA : public Message {
      void handle(ComponentA *c) {
        c->executeFunctionABC();
      }
    
      void handle(ComponentB *c) {
        ...
      }
    }
    

    This works well if you have a few components with very different functionality. The interface approach works well if you have components with similar functionality.


    Edit 2: To completely decouple the components, you can use a hybrid of the previous two solutions:

    class MessageHandler {
      virtual void handle(MessageA *msg) = 0;
      virtual void handle(MessageB *msg) = 0;
    }
    
    class MessageA : public Message {
      void handle(MessageHandler *handler) {
        handler->handle(this);
      }
    }
    
    class ComponentA : public MessageHandler {
      void handle(MessageA *msg) {
        executeFunctionABC();
      }
    }
    

    You still end up with an interface for the components, but you only have as many methods as there are messages. This is effectively what your preprocessor directive achieves.

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

Sidebar

Related Questions

we have this scenario: A server which contains needed data and client component which
Please consider such scenerio: I have component called TMenuItemSelector which has two published properties:
Scenario I have a DevExpress DataGrid which is bound to a DataSet in C#.
I have a .net application calling to a COM component (C++) which in turn
Suppose I have Project X under closed source. It references/links to component Y which
Scenario: I have a project containgin two C# projects, which for historical reasons must
All, I'm looking for advice over the following scenario: I have a component running
I have a simplified test scenario useful for asking this question: A Product can
I have an ASPX based component which I'd need to inlude into a plain
I have a scenario configuring Spring Security on embedded Jetty which seems to be

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.