• 【网摘】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).

  • 相关阅读:
    Javascript学习笔记3 Javascript与BOM简介
    Javascript学习笔记2.3 Javascript与DOM实现动态表格效果
    Javascript学习笔记2.2 Javascript与DOM选项卡(滑动门)案例详解
    javascript是做什么的
    Javascript学习笔记2.1 Javascript与DOM简介
    Javascript学习笔记1 javascript的特点
    CSS3新增的选择器和属性
    DNSlog实现Mysql注入
    [转]Firefox+Burpsuite抓包配置(可抓取https)
    爬虫初窥day3:BeautifulSoup
  • 原文地址:https://www.cnblogs.com/fangwenyu/p/1612764.html
Copyright © 2020-2023  润新知