J
jmls
Guest
so, lemme get this straight. Class B has a dependency on an "logger" class. #1 so, normal OO code is /** class B */ def var myLogger as Logger no-undo. myLogger = new Logger(). [snip] delete object myLogger. /** or wait for GC */ we both agree that this is bad. Tight coupling, difficult to test, etc #2 service locater code is /** class B */ def var _myLogger as ILogger no-undo. /** define as an interface */ _myLogger = LoggerFactory:instance() [snip] delete object myLogger. /** or wait for GC */ #3 DI code is /** class B */ def var _myLogger as Ilogger no-undo. constructor B(p_logger as ILogger): _myLogger = p_logger. end constructor. [snip] Now, I know that #2 is widely considered to be an anti-pattern (bad boy! don't do this) for a couple of reasons, the primary being that you cannot determine the dependencies of the class *however* , now we need another class to instantiate B , in order to pass in the appropriate parameters to the constructor . Where does class A get the instances to pass to the new instance of class B ? From a service locator / factory ? Also - who now determines the lifetime of the instances create by A to in order to pass to B ? Is it still B ? If so, then we now have to explicitly delete the instances in the destructor of class B or we my get memory leaks I really want to use #3. It just seems (perhaps it's because we don't have a proper DI framework in openedge) that we are moving the usage of service locators to another level. But we're still using them.
Continue reading...
Continue reading...