- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Dependency Injection and Factory Pattern.
Factory and Dependency injection both are the design pattern which can be used to enhance loose coupling abilities between the software components.
Factory design pattern is used to create objects. But, injection and life cycle management of the object should be handled by programmer within the application. There is no way to configure everything in a single place. Therefore, programmers need to call object creation logic wherever it needed which eventually hinder the loose coupling abilities.
In DI design pattern, creation of object, injecting of the instance and life cycle management of the instance can be handled outside the code. In spring, Instantiation ,Injection and Lifecycle management are handled by IOC container.
Sr. No. | Key | Factory design pattern | DI design pattern |
---|---|---|---|
1 | Object Creation | It is used to create objects. We have separate Factory class which contains creation logic. | It is responsible for creation and injection of the object. |
2 | State Of the Object | It is responsible for creation of stateful objects. | It is responsible to create stateless objects |
3 | Runtime /Compile time | Create object at compile time | Configure objects at runtime |
4 | Code Change | In case of change in business requirements, object creation logic may be changed. | No code change required |
5 | M echanism | Class is dependent on factory method which in turn have dependency on concrete classes | Parent object and all dependent object can be created at single location |
Example of Factory pattern
static class PizzaFactory{ public Pizza buildPizza(String pizzaType){ ........ Pizza pizza= //create pizza based on type return pizza; } } static void Main(){ Pizza pizza = PizzaFactory.buildPizza("Greek style"); }
Example of Dependency Injection
public class CustomerExample { public Address address public CustomerExample(Address address) { this.address = address; } } public class Address { .............. }
- Related Articles
- Difference between IOC and Dependency Injection in Spring.
- Dependency Injection in C#
- Explain dependency injection in C#
- Difference Between Constructor Injection and Setter Injection in Spring
- How to implement dependency injection using Interface-based injection in C#?
- What is dependency injection in PHP?\n
- How to implement Dependency Injection using Property in C#?
- What are the different ways to implement dependency injection and their advantages in C#?
- Difference between Computer Vision and Pattern Recognition
- What is functional dependency and transitive dependency (DBMS)?
- Basic SQL Injection and Mitigation with Example
- How do we handle circular dependency between Python classes?
- What is SQL Injection?
- Explain about partial and fully functional dependency
- How to Combat Injection Attacks?

Advertisements