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..d5234b39cd --- /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 serviceName) { + + if (serviceName.equalsIgnoreCase("EmailService")) { + return new EmailService(); + } else if (serviceName.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..787f7e708c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/Main.java @@ -0,0 +1,22 @@ +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"); + String email = service.getMessageBody(); + System.out.println(email); + + service = ServiceLocator.getService("SMSService"); + String sms = service.getMessageBody(); + System.out.println(sms); + + service = ServiceLocator.getService("EmailService"); + String newEmail = service.getMessageBody(); + System.out.println(newEmail); + } +} 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..7e1a2349a1 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/service/locator/ServiceLocator.java @@ -0,0 +1,27 @@ +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 serviceName){ + + MessagingService service = cache.getService(serviceName); + + if(service != null){ + return service; + } + + InitialContext context = new InitialContext(); + MessagingService service1 = (MessagingService)context.lookup(serviceName); + cache.addService(service1); + return service1; + } +}