zoukankan      html  css  js  c++  java
  • 【网摘】Dependency injection and service locator

    Source: http://blog.vuscode.com/malovicn/archive/2007/01/19/dependency-injection-and-service-locator.aspx

    Dependency Injection (DI) -- 

    Dependency injection is an interface programming technique which is based on altering class behavior without changing the class internals.

    That’s been done by coding against interface inside of the class and exposing interface definition to code external to the component. There are 3 types (different techniques) of making dependency injection:

    (1) Constructor DI

    Constructor type dependency injection is based on an approach where the interface parameter which is referenced internally inside the class is been exposed through the constructor.

    The good side of this approach is that in the moment when the code inside of the PayCheck class accessing the interface property, that property is guaranteed to be initialized.

    The down side is that once injected value of the interface can not be altered during the life time of implementation class after the instance is been constructed.

     

    (2) Setter DI

    Setter type of DI pattern is enabling the injection of the interface implementation through the public property with set block. It is basically approach trying to cover the downside of constructor based approach.

    The good side in this approach is that it allows flexible changing of the interface implementation(on the fly).

    The down side is that when the code inside the class uses interface field, it is not guaranteed that the field wouldn’t be null.

    (3) Interface DI

    Interface DI is a dependency injection technique which is a kind of explicit defined setter type DI. The object would use the interface property has to support interface which defines the method which sets the interface property.

    Service Locator (SL) --

     

    Service locator is a design pattern similar to dependency injection in a meaning that allows altering the class behavior without altering the class code by coding against the interface inside of the class.

    The difference is that there is no direct exposure of interface to code outside of the class. Instead class is using “well known object” inside, to get from it desired interface implementation.

     

    For all three explained dependency injection techniques, the common is the fact that they are ignorant completely about where from implementation of the interface comes and when it would be initialized.

     

    Service locator is also a dependency injection technique, but the decision about the way and moment of the implementation is moved to the implementation class itself (instead of outside interface implementation definition)

     

    Service locator is usually either 3rd party framework or our special assembly, which can decide about the type of interface implementation based on some configuration settings or something similar. Basically the whole point in using service locator is to extract and abstract the interface implementation decision outside of the implementation class. It is not a good practice that implementation class is calling the Service Locator method by providing some method (like a Factor does) because implementation class should be ignorant about the Service Locator implementation.

     

     

    The good side in this approach is that the control is allowed to control the moment of interface variable definition. It still allows flexible changing of the interface implementation because the service location internals are abstracted from the implementing class level.

     

    The down side is that the implementation class is not so dumb any more and it has to take care about things outside of her scope ( call to ServiceLocator method).

     

     

    Conclusion:

     

    Dependency injection is a very powerful technique very much used in Test Driven Development (TDD).

  • 相关阅读:
    shell-条件测试
    51Nod 1279 扔盘子 (思维+模拟)
    51Nod 1042 数字0-9的数量(数位DP)
    Codeforces 1138B Circus (构造方程+暴力)
    51nod 1133 不重叠的线段 (贪心,序列上的区间问题)
    51nod 1091 线段的重叠(贪心)
    EOJ Monthly 2019.2 E 中位数 (二分+中位数+dag上dp)
    牛客练习赛39 C 流星雨 (概率dp)
    牛客练习赛39 B 选点(dfs序+LIS)
    Educational Codeforces Round 57
  • 原文地址:https://www.cnblogs.com/fangwenyu/p/1612764.html
Copyright © 2011-2022 走看看