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

  • 相关阅读:
    第二次刷题感受——路漫漫其修远兮,吾将上下而求索。
    刷题就是照镜子——第一次刷2008年初赛题感受
    第一次集训刷题感受
    我的第一个博客
    预测房价的回归问题
    电影评论的情感极性分析
    语音助手是这样子的(二)
    语音助手是这样子的(一)
    软工第一次作业
    2020软件工程第五次作业_第二组
  • 原文地址:https://www.cnblogs.com/fangwenyu/p/1612764.html
Copyright © 2011-2022 走看看