Dependency Injection Container in Swift

栏目: IT技术 · 发布时间: 4年前

内容简介:In this article I will talk about Dependency Injection Containers or DICs’ in swift. Im my previous article I talked aboutUsing DI helps decoupling a class, but does not fully solve the problem. What about the parent class who creates the child class? We j

How to loosely couple your whole project

Dependency Injection Container in Swift

Photo by Patrick Brinksma on Unsplash

In this article I will talk about Dependency Injection Containers or DICs’ in swift. Im my previous article I talked about how to decouple code in swift , and introduced four levels of the decoupling ladder, take a look if you are not sure what I mean. I have mentioned Dependency Injection or DI, as a method used to decouple classes from its’ dependencies, by injecting its dependencies from the outside world.

Using DI helps decoupling a class, but does not fully solve the problem. What about the parent class who creates the child class? We just move the coupling from the child class to the parent class. The parent class is now tightly coupled, lets look at an example

ParentClass creates both the serviceOne and serviceTwo . ServiceOne has a dependency to ServiceTwoProtocol and we can assume they are both loosely coupled or decoupled. ParentClass however is tightly coupled to ServiceOne and ServiceTwo , because it creates specific objects that can not be replaced. How then can we decouple ParentClass ?

One way is to inject ParentClass ’ dependencies, which in this case is serviceOne . Lets see how that would look like.

By injecting serviceOne we have made ParentClass loosely coupled! This is perfectly fine but lets think about this a bit deeper. We didn’t fully solve the issue, because now LooselyCoupledParentClass ’ parent will be tightly coupled. The class that creates the objects will always be tightly coupled to the created objects. We just passed on the problem to some other class. How then can we decouple as many classes as possible?

In a complex project there may be many classes, each one with its own dependencies. These classes will probably be ordered into a complex hierarchy. The creation of the objects will probably be scattered around, causing many classes to be tightly coupled.

In order to avoid this, we can create one object which is responsible for creating all other objects. You may have guessed it, I’m referring to the dependency injection container object, lets take a look at what it would look like.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

精彩绝伦的CSS

精彩绝伦的CSS

[美] Eric A. Meyer / 姬光 / 人民邮电出版社 / 2012-7 / 49.00元

内容简介: 打造现代布局的专业技术 本书远非只是介绍基础知识,它不仅全面细致地讲解布局与效果,而且展望了HTML5和CSS3的未来。业内很少有人能像Eric A. Meyer一样详细阐明CSS,他在本书中深入分析了普遍适用的实用技术,讲解了如何选用正确的工具、如何通过jQuery使用CSS效果和CSS3技术。 本书主要内容如下: 显示或隐藏元素 通过XHTML为bod......一起来看看 《精彩绝伦的CSS》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具