0

OK, so signals and slots allow loose coupling connectivity between an object and method to another object and its method. This is fine but these signal slots objects exist as public members of a parent class and so their exposure undermines the loose coupling to create tightly coupled code. This is rather a negative thing. Shouldn't signals be made private so the parent class handles the connection/disconnection of the signal?

Musky
  • 91
  • 1
  • 6

1 Answers1

1

Well, something needs to know that when eventFoo happens on ObjectA, handlerBar on ObjectB needs to be invoked.

In the simplest, delegate-free implementation, that responsibility is on ObjectA, which calls ObjectB->handlerBar in its eventFoo body. That requires it to know about ObjectB, and to know that ObjectB->handlerBar is the right thing to call. ObjectB, in contrast, doesn't need to know about ObjectA.

In one method of using signals and slots, the responsibility is on ObjectB. It is responsible for registering its handlerBar as a handler for the eventFoo signal. So it needs to know about ObjectA, but ObjectA doesn't need to know about ObjectB.

In another method, ObjectB exposes handlerBar as a slot, and some other code connects eventFoo to handlerBar. That "other code" needs to know about both ObjectA and ObjectB, but neither one needs to know about the other.

So when someone talks about signals and slots reducing coupling, what they mean is that the event generating objects, and the event handling objects, are no longer explicitly coupled to each other, in the sense that one of their responsibilities is maintaining that coupling. Instead, the coupling is performed by a separate piece of code which is responsible for the coupling, and which is independent of the rest of the objects' interface.

Now, you mentioned "parent class", which makes me think you're thinking in terms of a situation where one of the classes is already coupled to the other. In that situation, public signals and slots are less of a win in terms of reducing coupling.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • Thanks for answer. 2nd paragraph is the approach I best favour. With regard to parent class, I mean the class that contains the signal object. I believe the signal object should be private as with any class member variable. – Musky Oct 31 '14 at 10:58
  • And so rather than one objecta coupling to object, a piece of coupling code needs to be provided. – Musky Nov 14 '14 at 15:03