From 78a6c223eb8775c8e360c7f038cc3f0fff96c614 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 20 Apr 2018 10:11:59 +0400 Subject: [PATCH 1/4] service locator pattern --- .../designpatterns/service/locator/Cache.java | 39 +++++++++++++++++++ .../service/locator/EmailService.java | 15 +++++++ .../service/locator/InitialContext.java | 17 ++++++++ .../designpatterns/service/locator/Main.java | 12 ++++++ .../service/locator/MessagingService.java | 8 ++++ .../service/locator/SMSService.java | 15 +++++++ .../service/locator/ServiceLocator.java | 26 +++++++++++++ 7 files changed, 132 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/service/locator/Cache.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/service/locator/EmailService.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/service/locator/InitialContext.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/service/locator/MessagingService.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/service/locator/SMSService.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Cache.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Cache.java new file mode 100644 index 0000000000..847d8a71e0 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Cache.java @@ -0,0 +1,39 @@ +package com.baeldung.designpatterns.service.locator; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Gebruiker on 4/20/2018. + */ +public class Cache { + private List services; + + public Cache(){ + services = new ArrayList(); + } + + public MessagingService getService(String serviceName){ + + for (MessagingService service : services) { + if(service.getServiceName().equalsIgnoreCase(serviceName)){ + System.out.println("Returning cached " + serviceName + " object"); + return service; + } + } + return null; + } + + public void addService(MessagingService newService){ + boolean exists = false; + + for (MessagingService service : services) { + if(service.getServiceName().equalsIgnoreCase(newService.getServiceName())){ + exists = true; + } + } + if(!exists){ + services.add(newService); + } + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/EmailService.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/EmailService.java new file mode 100644 index 0000000000..7ac7e05790 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/EmailService.java @@ -0,0 +1,15 @@ +package com.baeldung.designpatterns.service.locator; + +/** + * Created by Gebruiker on 4/20/2018. + */ +public class EmailService implements MessagingService { + + public String getMessageBody() { + return "email message"; + } + + public String getServiceName() { + return "EmailService"; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/InitialContext.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/InitialContext.java new file mode 100644 index 0000000000..da11c0c681 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/InitialContext.java @@ -0,0 +1,17 @@ +package com.baeldung.designpatterns.service.locator; + +/** + * Created by Gebruiker on 4/20/2018. + */ +public class InitialContext { + + public Object lookup(String jndiName) { + + if (jndiName.equalsIgnoreCase("EmailService")) { + return new EmailService(); + } else if (jndiName.equalsIgnoreCase("SMSService")) { + return new SMSService(); + } + return null; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java new file mode 100644 index 0000000000..3efc1a003c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java @@ -0,0 +1,12 @@ +package com.baeldung.designpatterns.service.locator; + +/** + * Created by Gebruiker on 4/20/2018. + */ +public class Main { + + public static void main(String[] args) { + MessagingService service = ServiceLocator.getService("EmailService"); + service.getMessageBody(); + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/MessagingService.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/MessagingService.java new file mode 100644 index 0000000000..5853dfd7f7 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/MessagingService.java @@ -0,0 +1,8 @@ +package com.baeldung.designpatterns.service.locator; + +public interface MessagingService { + + String getMessageBody(); + + String getServiceName(); +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/SMSService.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/SMSService.java new file mode 100644 index 0000000000..87255ef658 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/SMSService.java @@ -0,0 +1,15 @@ +package com.baeldung.designpatterns.service.locator; + +/** + * Created by Gebruiker on 4/20/2018. + */ +public class SMSService implements MessagingService { + + public String getMessageBody() { + return "sms message"; + } + + public String getServiceName() { + return "SMSService"; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java new file mode 100644 index 0000000000..b03077cc19 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java @@ -0,0 +1,26 @@ +package com.baeldung.designpatterns.service.locator; + +/** + * Created by Gebruiker on 4/20/2018. + */ +public class ServiceLocator { + private static Cache cache; + + static { + cache = new Cache(); + } + + public static MessagingService getService(String jndiName){ + + MessagingService service = cache.getService(jndiName); + + if(service != null){ + return service; + } + + InitialContext context = new InitialContext(); + MessagingService service1 = (MessagingService)context.lookup(jndiName); + cache.addService(service1); + return service1; + } +} From 81b9a8971e0ba82cbe22967d1cd71b2fba536b7f Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 20 Apr 2018 21:38:28 +0400 Subject: [PATCH 2/4] service locator pattern --- .../com/baeldung/designpatterns/service/locator/Main.java | 8 +++++++- .../designpatterns/service/locator/ServiceLocator.java | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java index 3efc1a003c..177ee66a75 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java @@ -6,7 +6,13 @@ package com.baeldung.designpatterns.service.locator; public class Main { public static void main(String[] args) { + MessagingService service = ServiceLocator.getService("EmailService"); - service.getMessageBody(); + String email = service.getMessageBody(); + System.out.println(email); + + service = ServiceLocator.getService("SMSService"); + String sms = service.getMessageBody(); + System.out.println(sms); } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java index b03077cc19..14123b33c5 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java @@ -4,6 +4,7 @@ package com.baeldung.designpatterns.service.locator; * Created by Gebruiker on 4/20/2018. */ public class ServiceLocator { + private static Cache cache; static { From e269f7e5999ab9ecbd1fba2637236c038d92e8f4 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Sun, 22 Apr 2018 17:13:38 +0400 Subject: [PATCH 3/4] variable name changes --- .../designpatterns/service/locator/InitialContext.java | 6 +++--- .../designpatterns/service/locator/ServiceLocator.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/InitialContext.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/InitialContext.java index da11c0c681..d5234b39cd 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/InitialContext.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/InitialContext.java @@ -5,11 +5,11 @@ package com.baeldung.designpatterns.service.locator; */ public class InitialContext { - public Object lookup(String jndiName) { + public Object lookup(String serviceName) { - if (jndiName.equalsIgnoreCase("EmailService")) { + if (serviceName.equalsIgnoreCase("EmailService")) { return new EmailService(); - } else if (jndiName.equalsIgnoreCase("SMSService")) { + } else if (serviceName.equalsIgnoreCase("SMSService")) { return new SMSService(); } return null; diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java index 14123b33c5..7e1a2349a1 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java @@ -11,16 +11,16 @@ public class ServiceLocator { cache = new Cache(); } - public static MessagingService getService(String jndiName){ + public static MessagingService getService(String serviceName){ - MessagingService service = cache.getService(jndiName); + MessagingService service = cache.getService(serviceName); if(service != null){ return service; } InitialContext context = new InitialContext(); - MessagingService service1 = (MessagingService)context.lookup(jndiName); + MessagingService service1 = (MessagingService)context.lookup(serviceName); cache.addService(service1); return service1; } From 313186ceb79d4053bfffc51ea3821101a6591f37 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Tue, 24 Apr 2018 09:22:20 +0400 Subject: [PATCH 4/4] testing changes --- .../com/baeldung/designpatterns/service/locator/Main.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java index 177ee66a75..787f7e708c 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java @@ -14,5 +14,9 @@ public class Main { service = ServiceLocator.getService("SMSService"); String sms = service.getMessageBody(); System.out.println(sms); + + service = ServiceLocator.getService("EmailService"); + String newEmail = service.getMessageBody(); + System.out.println(newEmail); } }