Difference IoC container and Factory pattern

来源:互联网 发布:华为媒体算法工程师 编辑:程序博客网 时间:2024/06/06 11:24

   1 I would suggest to keep the concepts plain and simple. Dependency Injection is more of a architectural pattern for loosely coupling software components. Factory pattern is just one way to separate the responsibility of creating objects of other classes to another entity. Factory pattern can be called as a tool to implement DI. Dependency injection can be implemented in many ways like DI using constructors, using mapping xml files etc.


    2 Inversion of Control (IoC) and Dependency Injection (DI) usually refer to the same thing. IoC is a more general concept (instead of doing X itself, a class relies on some other class to do it) DI is IoC as applied to dependent classes (instead of creating a class that I depend on, it will be passed to me). DI is the most common use of IoC, so usually when they are discussed the terms are used interchangeably.


The Factory Pattern's main concern is creating things - DI's main concern is how things are connected together.

In practice, these two concerns (creating and connecting) are usually related, so most DI/IoC containers do both. You could think of most implementations as very configurable factories - but because the implementations address both concerns - not because the Factory pattern and DI/IoC are the same.

原创粉丝点击