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.


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

查看所有标签

猜你喜欢:

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

智能革命

智能革命

李彦宏 等 / 中信出版集团 / 2017-5-1 / 68.00元

人类历史上的历次技术革命,都带来了人类感知和认知能力的不断提升,从而使人类知道更多,做到更多,体验更多。以此为标准,李彦宏在本书中将人工智能定义为堪比任何一次技术革命的伟大变革,并且明确提出,在技术与人的关系上,智能革命不同于前几次技术革命,不是人去适应机器,而是机器主动来学习和适应人类,并同人类一起学习和创新这个世界。“人工智能”正式写入2017年政府工作报告,折射出未来人工智能产业在我国经济发......一起来看看 《智能革命》 这本书的介绍吧!

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

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器