I have two components, A and B.
Component B requires that A has a certain state.
I can write this as part of B’s code,
or I can write this as part of A’s code (and maybe add assertions to B)
What should I take into consideration when making such a decision?
Edit
In this scenario there might be several B-type components.
It’s also assumed that I can’t avoid this situation
Edit 2
This often happens when working with frameworks. I usually have a some sort of “global settings”, and components that require those settings to be something
Possibilities:
Generally, the first solution is used, because ALL Bs refer to A, but A doesn’t actually have to know about ALL Bs (you said there were many). In theory, every object should do what it is supposed to do, ignoring anything else exists, unless its a controller object.
With the first solution, B checks what A has.
With the second solution, A becomes the controller of all Bs.
I would say that it’s better to have Bs check for A on creation, but in special cases, like when A is your main controller class, it MIGHT be preferable to have A create B.
Edit as response to Edit 2 by OP
Yes, in this case it’s almost always better to have B check in global settings. Global settings are there so you can check them! The only exception is if A is also the owner of all other components (such as the Game class in XNA)… Even there it would be difficult to choose and just to keep the architecture intact I’d still make B check inside of A, it’s just more clean and healthy.