Thread: Messaging with composition

  1. #1
    Chad Johnson
    Join Date
    May 2004
    Posts
    154

    Question Messaging with composition

    *EDIT: a better title probably may have been, "Messaging between objects"*

    Suppose I have the following (please forgive syntax errors):
    Code:
    class Module {
      public:
        Component *getComponent(type) {
          switch (type) {
            case "whatever":
              return new WhateverComponent();
              break;
          }
        }
    
        virtual String getModuleName();
    }
    
    class MyModule : public Module {
      public:
        String getModuleName() { return "MyModule"; }
    }
    
    class Component {
      void getContent() {
        ...
        // I want to call Module->getModuleName() here
        ...
      }
    }
    
    class SubComponent : public Component {
      ...
    }
    
    
    Module module = new MyModule();
    Component component = module->getComponent();
    String content = component->getBody();
    How do I get the module name (via getModuleName()) inside getContent() in Component to get the name associated with the Module subclass that instantiated the component?

    Would a good approach be to pass a pointer or reference to the Module (using "this") in the Component constructor in getComponent() when instantiating the Component?
    Code:
    class Module {
        ...
        Component *getComponent(type) {
          switch (type) {
            case "whatever":
              return new WhateverComponent(this);
              break;
          }
        }
        ...
    }
    
    class Component {
      private:
        Module *module;
    
      void getContent() {
        ...
        cout << this->module->getModuleName();
        ...
      }
    }
    Any better approach(es)?
    Last edited by ChadJohnson; 01-22-2008 at 11:20 PM.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    That's what I was going to suggest. Make Component class objects have a pointer to their parent module.

  3. #3
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    Thank you for the feedback.

    Let's suppose there is only going to be one instance of Module at any given time. What would you think about, instead of passing a reference or pointer to the Module object to the Component object's constructor when it is created in getComponent(), passing a reference or pointer to some global "Context" object which would have as a member variable (and an accessor function) that one instance of Module? This "Context" object could also provide access to other main settings for the application, like a database link.
    Last edited by ChadJohnson; 01-23-2008 at 04:24 AM.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    I would just make the module a static pointer to Module.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-10-2007, 03:51 PM
  2. Simple Instant Messaging
    By mrafcho001 in forum Tech Board
    Replies: 1
    Last Post: 03-30-2006, 05:04 PM
  3. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  4. Simulation of electronic messaging
    By c_help in forum C Programming
    Replies: 9
    Last Post: 02-19-2005, 11:18 AM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM