Closed Thread
Results 1 to 2 of 2

Thread: Object Oriented dilemma: Two objects communicating. Event based or direct?

  1. #1
    Zimmer is offline Newbie
    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

    Thumbs up Object Oriented dilemma: Two objects communicating. Event based or direct?

    Hi Codecall regulars,

    I'm not really new to OOP but I haven't really got my head around it fully. I have a problem that is probably best illustrated with a simple example, I'm sure this is a very common issue.

    Let's say I have a Car() class, which instantiates other classes such as Wheels(), Engine() and so forth.

    In the Car()'s mainloop, let's say it calls Accelerator.GoFaster(). The accelerator object updates itself but it has to go and tell the engine to open up its throttle. It would be inefficient to have Car()'s mainloop checking whether this needs to be done every cycle, but by my understanding it would be poor OOP practice for the accelerator to directly address the engine class through something like parent.myEngine.openThrottle().

    How can the engine instance be informed of the increased acceleration without checking every cycle, and while avoiding the messy solution of having the Accelerator() go back up through parent and down to the engine instance?

    Something event-based perhaps?

    Any ideas?

    I'm sure this problem is dead obvious to many of you, I just can't seem to Google up any good material on it!

    Thanks for any insight you may have.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Maurice_Z is offline Learning Programmer
    Join Date
    Nov 2007
    Location
    Poland
    Posts
    41
    Rep Power
    0

    Re: Object Oriented dilemma: Two objects communicating. Event based or direct?

    Well, If you have more than one car, I'd say event basing is wrong way to do it, as it would only slow down the whole project. In my humble opinion the best thing to do would be either:
    - Have functions in Car, so you have to call Parent.OpenThrottle() - The worse solution
    - Have direct link to myEngine in Accelerator, so in Accelerator you only have to call Engine.openThrottle() - the better one
    If you are planning to have really lots of cars I'd consider keeping all the stuff in one object, ie Car, to make it most efficient. But it depends largely of what the final outcome should look.
    I'm but a young games developer. If I were in your place, I'd just try all three ways to see which is fastest.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C# Object Oriented How To Help
    By simoatk in forum C# Programming
    Replies: 0
    Last Post: 08-17-2010, 09:01 AM
  2. Object Oriented programming
    By eman ahmed in forum Java Help
    Replies: 10
    Last Post: 08-12-2010, 03:59 AM
  3. object oriented programming
    By farhanyun91 in forum Java Help
    Replies: 6
    Last Post: 08-11-2010, 09:28 AM
  4. Object Oriented Techniques
    By student_oop in forum General Programming
    Replies: 17
    Last Post: 02-14-2010, 01:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts