设计模式学习—服务定位模式(Service Locator Design Pattern)

一、我的理解


服务定位模式主要针对JNDI(Java Naming and Directory Interface, Java命名和目录接口)。利用缓存技术使得用户在访问统一服务时,可以直接从缓存中取出对象,不用重新创建。

二、实现方式


利用缓存,当用户访问某一服务时,先从缓存中检查是否有该服务对象,如果没有再重新创建该对象。

三、实例

设计模式学习—服务定位模式(Service Locator Design Pattern)_第1张图片
Java实例

Service接口:向外暴露的服务接口

package com.study.dp.servicelocator;

public interface Service {

    public String getName();
    public void execute();
    
}
Service1类:实际实现的服务一

package com.study.dp.servicelocator;

public class Service1 implements Service {

    @Override
    public String getName() {
        return "Service1";
    }

    @Override
    public void execute() {
        System.out.println("Executing Service1");
    }

}
Service2类:实际实现的服务二

package com.study.dp.servicelocator;

public class Service2 implements Service {

    @Override
    public String getName() {
        return "Service2";
    }

    @Override
    public void execute() {
        System.out.println("Executing Service2");
    }

}
InitialContext类:初始化上下文,即服务的提供方

package com.study.dp.servicelocator;

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;
    }
    
}
Cache类:缓存类

package com.study.dp.servicelocator;

import java.util.ArrayList;
import java.util.List;

public class Cache {

    private List services;
    
    public Cache() {
        services = new ArrayList<>();
    }
    
    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;
                break;
            }
        }
        if(!exists) {
            services.add(newService);
        }
    }
    
}
ServiceLocator类:服务定位类,该类负责获取对应服务对象

package com.study.dp.servicelocator;

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 = (Service)context.lookup(jndiName);
        cache.addService(service);
        return service;
    }
    
}
Demo类:测试类

package com.study.dp.servicelocator;

public class Demo {

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


四、应用场景






你可能感兴趣的:(设计模式)