File tree Expand file tree Collapse file tree 7 files changed +143
-0
lines changed
core-java/src/main/java/com/baeldung/designpatterns/service/locator Expand file tree Collapse file tree 7 files changed +143
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .baeldung .designpatterns .service .locator ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+
6
+ /**
7
+ * Created by Gebruiker on 4/20/2018.
8
+ */
9
+ public class Cache {
10
+ private List <MessagingService > services ;
11
+
12
+ public Cache (){
13
+ services = new ArrayList <MessagingService >();
14
+ }
15
+
16
+ public MessagingService getService (String serviceName ){
17
+
18
+ for (MessagingService service : services ) {
19
+ if (service .getServiceName ().equalsIgnoreCase (serviceName )){
20
+ System .out .println ("Returning cached " + serviceName + " object" );
21
+ return service ;
22
+ }
23
+ }
24
+ return null ;
25
+ }
26
+
27
+ public void addService (MessagingService newService ){
28
+ boolean exists = false ;
29
+
30
+ for (MessagingService service : services ) {
31
+ if (service .getServiceName ().equalsIgnoreCase (newService .getServiceName ())){
32
+ exists = true ;
33
+ }
34
+ }
35
+ if (!exists ){
36
+ services .add (newService );
37
+ }
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .designpatterns .service .locator ;
2
+
3
+ /**
4
+ * Created by Gebruiker on 4/20/2018.
5
+ */
6
+ public class EmailService implements MessagingService {
7
+
8
+ public String getMessageBody () {
9
+ return "email message" ;
10
+ }
11
+
12
+ public String getServiceName () {
13
+ return "EmailService" ;
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .designpatterns .service .locator ;
2
+
3
+ /**
4
+ * Created by Gebruiker on 4/20/2018.
5
+ */
6
+ public class InitialContext {
7
+
8
+ public Object lookup (String serviceName ) {
9
+
10
+ if (serviceName .equalsIgnoreCase ("EmailService" )) {
11
+ return new EmailService ();
12
+ } else if (serviceName .equalsIgnoreCase ("SMSService" )) {
13
+ return new SMSService ();
14
+ }
15
+ return null ;
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .designpatterns .service .locator ;
2
+
3
+ /**
4
+ * Created by Gebruiker on 4/20/2018.
5
+ */
6
+ public class Main {
7
+
8
+ public static void main (String [] args ) {
9
+
10
+ MessagingService service = ServiceLocator .getService ("EmailService" );
11
+ String email = service .getMessageBody ();
12
+ System .out .println (email );
13
+
14
+ service = ServiceLocator .getService ("SMSService" );
15
+ String sms = service .getMessageBody ();
16
+ System .out .println (sms );
17
+
18
+ service = ServiceLocator .getService ("EmailService" );
19
+ String newEmail = service .getMessageBody ();
20
+ System .out .println (newEmail );
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .designpatterns .service .locator ;
2
+
3
+ public interface MessagingService {
4
+
5
+ String getMessageBody ();
6
+
7
+ String getServiceName ();
8
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .designpatterns .service .locator ;
2
+
3
+ /**
4
+ * Created by Gebruiker on 4/20/2018.
5
+ */
6
+ public class SMSService implements MessagingService {
7
+
8
+ public String getMessageBody () {
9
+ return "sms message" ;
10
+ }
11
+
12
+ public String getServiceName () {
13
+ return "SMSService" ;
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .designpatterns .service .locator ;
2
+
3
+ /**
4
+ * Created by Gebruiker on 4/20/2018.
5
+ */
6
+ public class ServiceLocator {
7
+
8
+ private static Cache cache ;
9
+
10
+ static {
11
+ cache = new Cache ();
12
+ }
13
+
14
+ public static MessagingService getService (String serviceName ){
15
+
16
+ MessagingService service = cache .getService (serviceName );
17
+
18
+ if (service != null ){
19
+ return service ;
20
+ }
21
+
22
+ InitialContext context = new InitialContext ();
23
+ MessagingService service1 = (MessagingService )context .lookup (serviceName );
24
+ cache .addService (service1 );
25
+ return service1 ;
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments