Merge pull request #4060 from MherBaghinyan/BAEL-1718

Bael 1718
This commit is contained in:
Loredana Crusoveanu 2018-04-26 00:18:47 +03:00 committed by GitHub
commit 594ebafdff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 143 additions and 0 deletions

View File

@ -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);
}
}
}

View File

@ -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";
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.designpatterns.service.locator;
public interface MessagingService {
String getMessageBody();
String getServiceName();
}

View File

@ -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";
}
}

View File

@ -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;
}
}