-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIocContainer.java
More file actions
48 lines (39 loc) · 1.19 KB
/
IocContainer.java
File metadata and controls
48 lines (39 loc) · 1.19 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
48
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 IocContainer {
public MessageService getMessageService(String type) {
if(type.equals("SMS")) return new SmsService();
else if(type.equals("Email")) return new EmailService();
return null;
}
public NotificationService getNotificationService(String type) {
return new NotificationService(getMessageService(type));
}
public static void main(String[] args) {
IocContainer container = new IocContainer();
container.getNotificationService("SMS").notifyUser();
container.getNotificationService("Email").notifyUser();
}
}