Object Oriented Design

I think I understand the spirit of OOP, but when it comes to the actual implentation, things get fuzzy...

For example, I have a window class ui file that has all the user interface aspects (buttons, text boxes, etc)

Now suppose there's a totally unrelated class that, with objects that exist independent of the display.

How would the non-gui object’s function be called from a button press? Even though unrelated, should the windows class inherit from these non-window type classes? Is there a typical way of architecting such a (common?) scenario?
How would the non-gui object’s function be called from a button press? Even though unrelated, should the windows class inherit from these non-window type classes?


Absolutely not.

The idea of OOP is that each class clearly represents a thing, and nothing more. Like each class is an individual piece of a puzzle. Then the "big picture" code just puts those pieces together in a way which makes sense.

In this case, since the Window class and the non-Window class have nothing conceptually to do with each other... neither one of them should even be aware that the other one exists.

Whatever code owns the window object (that is... whatever code created the window and maintains the object) probably also owns the non-window object as well, right? In that case... that is the code that should be responsible for tying the 2 parts together.


The best way to do this depends on how the Window class notifies you of the button press. Often times, you can give the Window class a callback that will be called whenever the button is pressed. If that's the case, all you have to do is route the callback to call the non-Window class's trigger function. And if you're really clever, you can usually give it the trigger function directly as the callback.
window class ui file

Is that by any chance Qt's "ui-file"?
Composition describes one thing being made up of other things. And as Disch said, the things don't know about each other.
https://en.wikipedia.org/wiki/Object_composition
Topic archived. No new replies allowed.