服務(wù)定位器模式
服務(wù)定位器模式(Service Locator Pattern)用在我們想使用 JNDI 查詢定位各種服務(wù)的時(shí)候⊥ば螅考慮到為某個(gè)服務(wù)查找 JNDI 的代價(jià)很高,服務(wù)定位器模式充分利用了緩存技術(shù)猪钮。在首次請(qǐng)求某個(gè)服務(wù)時(shí)党饮,服務(wù)定位器在 JNDI 中查找服務(wù),并緩存該服務(wù)對(duì)象友酱。當(dāng)再次請(qǐng)求相同的服務(wù)時(shí)晴音,服務(wù)定位器會(huì)在它的緩存中查找,這樣可以在很大程度上提高應(yīng)用程序的性能缔杉。以下是這種設(shè)計(jì)模式的實(shí)體锤躁。
- 服務(wù)(Service) - 實(shí)際處理請(qǐng)求的服務(wù)。對(duì)這種服務(wù)的引用可以在 JNDI 服務(wù)器中查找到或详。
- Context / 初始的 Context - JNDI Context 帶有對(duì)要查找的服務(wù)的引用系羞。
- 服務(wù)定位器(Service Locator) - 服務(wù)定位器是通過(guò) JNDI 查找和緩存服務(wù)來(lái)獲取服務(wù)的單點(diǎn)接觸。
- 緩存(Cache) - 緩存存儲(chǔ)服務(wù)的引用鸭叙,以便復(fù)用它們觉啊。
- 客戶端(Client) - Client 是通過(guò) ServiceLocator 調(diào)用服務(wù)的對(duì)象拣宏。
實(shí)現(xiàn)
我們將創(chuàng)建 ServiceLocator沈贝、InitialContext、Cache勋乾、Service 作為表示實(shí)體的各種對(duì)象宋下。Service1 和 Service2 表示實(shí)體服務(wù)。
ServiceLocatorPatternDemo 類在這里是作為一個(gè)客戶端辑莫,將使用 ServiceLocator 來(lái)演示服務(wù)定位器設(shè)計(jì)模式学歧。
服務(wù)定位器模式.png
- 創(chuàng)建服務(wù)接口 Service。
Service.java
public interface Service {
public String getName();
public void execute();
}
- 創(chuàng)建實(shí)體服務(wù)各吨。
Service1.java
public class Service1 implements Service {
public void execute(){
System.out.println("Executing Service1");
}
@Override
public String getName() {
return "Service1";
}
}
Service2.java
public class Service2 implements Service {
public void execute(){
System.out.println("Executing Service2");
}
@Override
public String getName() {
return "Service2";
}
}
- 為 JNDI 查詢創(chuàng)建 InitialContext枝笨。
InitialContext.java
public class InitialContext {
public Object lookup(String jndiName){
if(jndiName.equalsIgnoreCase("SERVICE1")){
System.out.println("Looking up and creating a new Service1 object");
return new Service1();
}else if (jndiName.equalsIgnoreCase("SERVICE2")){
System.out.println("Looking up and creating a new Service2 object");
return new Service2();
}
return null;
}
}
- 創(chuàng)建緩存 Cache。
Cache.java
import java.util.ArrayList;
import java.util.List;
public class Cache {
private List<Service> services;
public Cache(){
services = new ArrayList<Service>();
}
public Service getService(String serviceName){
for (Service service : services) {
if(service.getName().equalsIgnoreCase(serviceName)){
System.out.println("Returning cached "+serviceName+" object");
return service;
}
}
return null;
}
public void addService(Service newService){
boolean exists = false;
for (Service service : services) {
if(service.getName().equalsIgnoreCase(newService.getName())){
exists = true;
}
}
if(!exists){
services.add(newService);
}
}
}
- 創(chuàng)建服務(wù)定位器揭蜒。
ServiceLocator.java
public class ServiceLocator {
private static Cache cache;
static {
cache = new Cache();
}
public static Service getService(String jndiName){
Service service = cache.getService(jndiName);
if(service != null){
return service;
}
InitialContext context = new InitialContext();
Service service1 = (Service)context.lookup(jndiName);
cache.addService(service1);
return service1;
}
}
- 使用 ServiceLocator 來(lái)演示服務(wù)定位器設(shè)計(jì)模式横浑。
ServiceLocatorPatternDemo.java
public class ServiceLocatorPatternDemo {
public static void main(String[] args) {
Service service = ServiceLocator.getService("Service1");
service.execute();
service = ServiceLocator.getService("Service2");
service.execute();
service = ServiceLocator.getService("Service1");
service.execute();
service = ServiceLocator.getService("Service2");
service.execute();
}
}
- 執(zhí)行程序,輸出結(jié)果:
Looking up and creating a new Service1 object
Executing Service1
Looking up and creating a new Service2 object
Executing Service2
Returning cached Service1 object
Executing Service1
Returning cached Service2 object
Executing Service2