Thread: Another design issue (and I don't even know how to make a thread title of it)

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Another design issue (and I don't even know how to make a thread title of it)

    Once again I'm stuck with some class design issue. I have a class that has a reference of another class or "extends" (I believe it's the correct word) another class. If that other class is changed, how can I get the class that referenced it to know that it has changed and triggered an execution of a method? Maybe it's confusing, but the pesudocode is pretty much like the code below:

    Code:
    class A
    {
      public:
            A(int a): IntA(a);
            void setInt(int theInt) 
            { 
                    mIntA = theInt; 
            }
            int getInt() 
            { 
                    return mIntA; 
            }      
           ~A();
      protected:
           int mIntA;
    }
    
    class B
    {
      public:
          B(A* theA, int mult)
          {
              mTheA = theA;
              mMultiplier = mult;
              mResult = theA->getInt() * mult;
          }
          void changeMult(int mult)
         {
            mMultiplier = mult;
            calc();
         };
          void calc()
         {
            mResult = mMultiplier * theA->getInt();
         };
      protected:
         A* mTheA;
         int mMultiplier;
         int mResult;
    }
    What I want to do is changing the integer in "varA" object and also trigger the "varB" which referenced it to call the method "calc()". So it's gonna be like this:

    Code:
    int main ()
    {
      A* varA = new A(1);
      B* varB = new B(A, 2); //A->mIntA=1 ; B->mMultiplier=2 ; B->mResult=2
      varB->changeMult(4); // B->mMultiplier=4: A->mIntA=1 ; B->mResult=4
      //Change var A and automatically call B->calc()
      varA->setInt(4) //B->mMultiplier=4; A->mIntA=4 ; B->mResult=4*4=16  
    }

    How do I do that?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You could do lazy evaluation, where you don't calculate mResult until you need to use it. For example, wait until somebody calls getResult and then call calc to make sure the calculation is up to date.

    Otherwise you have to do some complicated coding where the B class registers itself in the A class object to receive notifications that A has changed.

    In this example, you don't really need to store mResult anyway, you can always call calc() when you need that value instead and have it return the value instead of storing it internally.

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by Daved
    You could do lazy evaluation, where you don't calculate mResult until you need to use it. For example, wait until somebody calls getResult and then call calc to make sure the calculation is up to date.

    Otherwise you have to do some complicated coding where the B class registers itself in the A class object to receive notifications that A has changed.

    In this example, you don't really need to store mResult anyway, you can always call calc() when you need that value instead and have it return the value instead of storing it internally.
    I see your point. BTW, isn't there a Design Pattern to handle this? When one of my friends has a Design Pattern assignment (in my college days) to make a simple UML Editor, he uses it in the GUI. E.g. when the object's type changed from actor to class in the diagram view, the treeview changed also, and vice versa. I think the pattern's name was, CMIIW, "Observer". I forgot all about Design Patterns (and I don't quite understand it back then). But I think your method is sufficient for now. Thanks alot.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's indeed the Observer pattern that handles registration for change notification. Of course, you'd still need to write the code for it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed