What Is Dependency Injection in JAVA

11
8/12/2019 What Is Dependency Injection in JAVA http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 1/11 Apex T. G. India Pvt. Ltd Chapter 1 Dependency Injection Introduction

Transcript of What Is Dependency Injection in JAVA

Page 1: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 1/11

Apex T. G. India Pvt. Ltd

Chapter 1

Dependency Injection

Introduction

Page 2: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 2/11

1

Dependency injection

Dependency injection is a jargon created by Martin Fowler and it is also called as inversion of control.

In object oriented design, objects have relationship with one another. A class (A) can have attributes ( )

and !ethods. "hose attributes are again instances of another class (#). If class (A) wants to wor$ and

perfor! its objective, attributes ( ) should be instantiated.

Page 3: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 3/11

1

Dependency injection

"here are different ways to instantiate an object and we have seen a lot in our design pattern tutorial

series. A si!ple and direct way is to use the %new& operator and call the constructor of #lass (#) where

we need that instance in class (A). "his is class A has absolute control over creation of attribute ( ). It

decides which class (#) to call and how to call etc.

Page 4: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 4/11

1

Dependency injection

'ow, if we outsource that instantiation and supplying an instance job to so!e third party. #lass (A)

needs instance of class (#) to operate, but it out sources that responsibility to so!e third party. "he

designated third party, decides the !o!ent of creation and the type to use to create the instance.

Page 5: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 5/11

1

Dependency injection

"he dependency between class (A) and class (#) is injected by a third party. *hole of this agree!ent

involves so!e con+iguration infor!ation too. "his whole process is called dependency injection.

Page 6: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 6/11

1

DI FD-

Factory design pattern(FD-) and dependency injection(DI) !ay loo$ related but we loo$ at the! with

!icroscope then we can understand the are different. ven if we use a factory the dependent class has

the responsibility of creating the instance but the core of dependency injection is separating that

responsibility to e/ternal co!ponent.

Page 7: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 7/11

1

Dependency Injection

#lass A 0

private # obj1

public voidso!eMethod(# obj) 0

...

this.obj2 obj1

...

3

3

Page 8: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 8/11

1

Factory Design -attern

#lass A 0

private # obj1

public voidso!eMethod() 0

...

this.obj2My4bjectFactory.get#()1

...

3

3

Page 9: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 9/11

1

DI Advantage

5oosely couple architecture.

6eparation of responsibility

#on+iguration and code is separate.

7sing con+iguration, a different i!ple!entation can be supplied without changing the dependent code.

"esting can be perfor!ed using !oc$ objects.

Page 10: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 10/11

1

DI "ype

Dependency injection is classi+ied into three categories na!ely8..

#onstructor Injection

6etter Injection

Interface9based Injection

Page 11: What Is Dependency Injection in JAVA

8/12/2019 What Is Dependency Injection in JAVA

http://slidepdf.com/reader/full/what-is-dependency-injection-in-java 11/11

Thanks

facebook.com/apex.tgi

twitter.com/ApextgiNoida

pinterest.com/apextgi

Stay Connected with us for more chapters on JAVA