OOP - is it better to pass big controller objects to children, or have children dispatch events? -
i'm experienced programmer, i've struggled particular issue.. have main class maybe displays pop-ups on else, transitions between screens , forth, , have screen objects themselves, , pop-up objects. when click "close" on pop-up, or click "go new screen" on screen, or whatever may be, these objects need communicate main class , tell stuff. , need pass data main class aswell. in experience work out better have children dispatch events data, main class picks up, or somehow pass main class down children through constructors, , have bunch of public methods in main class children can call? or both equally valid?
edit: , also, made me post this: in game, user goes through bunch of different menu screens, , in each screen, adds game-config object @ end used generate gamescreen. dont want keep saying dispatchevent("addvaluetogameconfig", value) or something, want bigcontroller.gameconfig.value = "whatever";
if hierarchy of children deep, go events or observer. leads lower coupling between classes. substantial refactoring of main class can have huge avalanche effect on dependent children. become knowledgeable (higher change of breaking law of demeter) parent , take on additional responsibility (arguably breaking srp) besides managing own state, of informing own state in custom manner compatible parent. firing specialized meaningful notification state change "space", not arbitrary property change, releases them worrying parent's state. parent's duty informed changes of state of children. furthermore, having set of meaningful events can lead narrower public interface of children. may readability, not having go , down through hierarchy.
Comments
Post a Comment