commit
594ebafdff
|
@ -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<MessagingService> services;
|
||||
|
||||
public Cache(){
|
||||
services = new ArrayList<MessagingService>();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.designpatterns.service.locator;
|
||||
|
||||
public interface MessagingService {
|
||||
|
||||
String getMessageBody();
|
||||
|
||||
String getServiceName();
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue