-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDI.java
More file actions
47 lines (47 loc) · 1.5 KB
/
DI.java
File metadata and controls
47 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
interface Injection {
void run(String user, String pass);
}
class ServiceProvider implements Injection {
@Override
public void run(String user, String pass) {
System.out.println(user + " " + " " + pass);
}
}
class Client {
private Injection service; // creating variable of type interface
//creating constructor (Client) has injected argument of type interface (Inejction)
public Client(Injection service){
this.service=service;
}
public void setService(Injection i){
if (i == null){
throw new RuntimeException("Please be more smart about it");
}
this.service = i;
}
//creating method that gives some stuff to the service class
public void test(){
service.run("user1", "pass1234");
}
}
public class DI {
public static void main(String[] args) {
//create object from the interface that has instance of serviceprovider
Injection a = new ServiceProvider();
//creating object of type client that has injected object
Client obj = new Client(a);
obj.test();
Injection b = new Injection() {
@Override
public void run(String user, String pass) {
System.out.println("Hi user " + " "+ user +" " + pass);
}
};
obj.setService(b);
obj.test();
/* if (){
Injection newService = new NewA();
obj.setService(newService);
}*/
}
}