-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyInjection.java
More file actions
40 lines (32 loc) · 954 Bytes
/
DependencyInjection.java
File metadata and controls
40 lines (32 loc) · 954 Bytes
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
interface MessageService {
public void send();
}
class EmailService implements MessageService {
@Override
public void send() {
System.out.println("Sending email message");
}
}
class SmsService implements MessageService {
@Override
public void send() {
System.out.println("Sending SMS message");
}
}
class NotificationService {
private final MessageService messageService;
public NotificationService(MessageService messageService) {
this.messageService = messageService;
}
public void notifyUser() {
messageService.send();
}
}
public class DependencyInjection {
public static void main(String[] args) {
NotificationService notificationService = new NotificationService(new EmailService());
notificationService.notifyUser();
notificationService = new NotificationService(new SmsService());
notificationService.notifyUser();
}
}