Dependency Injection Flashcards

(21 cards)

1
Q

singleton

A

The singleton pattern is a software design pattern that restricts the instantiation of a class to one “single” instance. This is useful when exactly one object is needed to coordinate actions across the system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

dependency injection replaces the…

A

“new” keyword to create an instance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

is a singleton a service?

A

yes - annotate a singleton service with @Service - we didn’t learn this yet for Dagger

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Dagger component

A

Dagger can create a graph of the dependencies in your project that it can use to find out where to get those dependencies when they are needed. To make Dagger do this, you have to create an interface and annotate it with @Component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

modules (in dependency injection)

A
classes where dependencies are defined for the class: ie CarModule contains definitions of dependencies for Car, like Engine or Wheels
-----
import
..
public class CarModule extends --- {
     @Provides
     public Engine providesEngine() {
          return new Engine();
     }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

@Provides annotation

A

this annotation preceeding a methods tells Dagger that this method describes a dependency

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

@Inject

A
annotation for the constructor
@Inject
public Car(Engine engine) {
     this.engine = engine;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

dependency injection …

A

…decouples your class’s construction from the construction of its dependencies

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

core idea of dependency injection:

A

provide the member variables for a class through its constructor, rather than letting the class instantiate its own objects –> using the @Inject annotated constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

when classes instantiate their own dependency objects…

A

…the dependency instantiation logic is mixed up with the actual class logic (hence, dependency injection)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

dependency injection separates the instantiation logic from…

A

…from the code that uses the instantiated object to get its work done. (We can code the instantiation logic any way we please in a separate “injection” class: locality, edge cases, happy case, etc)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

True or false: Dependence injection benefits unit testing as a kind of side effect.

A

True. By accepting the classes we need as arguments to our constructor, we can pass mocks in our unit tests, narrowing the scope of what our unit tests actually check to only the class functions we are testing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Dagger allows us to specify how to provide dependency objects using …..

A

…annotations (rather than full methods or classes - think App() ) that are in the familiar Java annotation format, i.e. JUnit’s @Test, @BeforeEach

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Dagger @Component

A

provider of the root object(s)

use provide* method(s) in the @Component interface to “start” the domino effect of instantiations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Dagger @Inject

A
specify how to construct dependencies
Dagger will look at the return type of the provide* method(s) in the @Component interface, go to that class and use the constructor which is annotated with @Inject
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Dagger requires us to annotate the specific constructor that it should use to instantiate an object of the class using which annotation?

A

@Inject
public ObjectConstructor(ObjectType1 objectType1, …
–>finds the class ObjectType1 and looks for the @Inject constructor, etc…
Note: no specific dagger method used! Just @Inject

17
Q

The interface provide* method…

A

returns the root object that your application needs

18
Q

@Component
public interface AppComponent {
public IceCreamService provideIceCreamService();
}
In the application, write two lines of code that a)retrieves the Dagger-generated component class, and b)returns an instance of the root class

A
//retrieve the Dagger-generated component class
AppComponent dagger= DaggerIceCreamService.create()
//return an instance of the root class
IceCreamService iceCreamService = dagger.provideIceCreamService();
19
Q

What is dependency injection with a real time example?

A

DI can exist between two objects, for instance, a flashlight and a battery. The flashlight needs the battery to function. However, any changes made to the battery, such as switching it another brand/set of batteries, does not ean the dependent object (flashlight) also needs to be changed.

20
Q

Is dependency injection used in Production?

A

Yes. Dependency Injection is an architecture pattern, not a testing one. So it is intended to be used when building production code.

21
Q

When we have a @Singleton in the dependency graph we need to annotate the @Component interface with @Singleton as well.
True or False?