OO design and communication between classes

C

Christopher

I have a class FrameListener, that contains a method which loops
continually, if this method returns false then the application exits.
I have a class KeyListener, that gets input from the user.

Both these classes are 3rd party and beyond my control. I can however derive
from these as long as the base functionality is intact. I did indeed derive
from these and make my own custom functionality on top.

My question is how can I design things so that the KeyListener can let the
FrameListener know when a user hits escape so that the FrameListener can
exit the application?

I am not sure whether to

(A) make a class derived from both FrameListener and KeyListener holding
some bool like m_continue_loopig

(B) keep them seperate as suggested by the 3rd party and make some
event/event handler design (C++ doesn't directly support this as far as I
know)

(C) have FrameListener contain KeyListener (i guess KeyListener would have
to take a FrameListener reference in its constructor)

(D) ...any suggestions?

I am sure this kind of communication between objects is common. What kind of
things should I ask myself and what designs should I consider?

Thanks.
Christopher
 
D

Donovan Rebbechi

(A) make a class derived from both FrameListener and KeyListener holding
some bool like m_continue_loopig

Very messy -- arguably an abuse of inheritance.
(B) keep them seperate as suggested by the 3rd party and make some
event/event handler design (C++ doesn't directly support this as far as I
know)

This is the most scalable option. When/if you have more objects that need
to talk to each other, the other solutions will start to get unwieldy. Given
what you've said about your third party toolkit, doesn't it have any support
for event handling ?
(C) have FrameListener contain KeyListener (i guess KeyListener would have
to take a FrameListener reference in its constructor)

This would work but wouldn't generalise very well. What happens when you have
more objects that need to talk to each other ? Any synchronisation code
that you need to make sure that two things don't write to an address at the
same time, and/or one thing isn't reading while another is writing, would
need to be duplicated whereas if you had some sort of general event model,
you could conveniently encapsulate this in a class.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top