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).

  • 相关阅读:
    Vue组件以及组件之间的通信
    VueRouter和Vue生命周期(钩子函数)
    Vuex、axios以及跨域请求处理
    element-ui和npm、webpack、vue-cli搭建Vue项目
    2018PyCharm激活方法
    pycharm修改选中字体颜色
    为自己的博客园添加目录锚点和返回顶部
    python初识
    JAVA判断当前日期是节假日还是工作日
    springmvc使用freemarker
  • 原文地址:https://www.cnblogs.com/fangwenyu/p/1612764.html
Copyright © 2011-2022 走看看