标签:标识 rate href bae and 设计模式 consumer lookup print
本文介绍服务定位器模式(Service Locator Pattern),并提供了 UML 图和示例 Java 代码。
服务定位器模式实现了按需返回服务实例。在该模式中,应用所有需要的服务都会被注册到服务定位器中,并通过 ID 唯一标识。应用需要哪个服务,用这个 ID 就能从服务定位器中得到这个服务的实例。服务定位器模式解耦了服务调用者和具体的服务实现。该模式的 UML 类图可以表示如下:
服务定位器模式包含如下组件:
让我们看一个具体的例子来理解服务定位器模式,准备代码如下:
// Java program to
// illustrate Service Design Service
// Locator Pattern
import java.util.ArrayList;
import java.util.List;
首先,定义服务 Service 接口以及两个具体服务实现:
// Service interface
// for getting name and
// Executing it.
interface Service {
public String getName();
public void execute();
}
// Service one implementing Locator
class ServiceOne implements Service {
public void execute()
{
System.out.println("Executing ServiceOne");
}
@Override
public String getName()
{
return "ServiceOne";
}
}
// Service two implementing Locator
class ServiceTwo implements Service {
public void execute()
{
System.out.println("Executing ServiceTwo");
}
@Override
public String getName()
{
return "ServiceTwo";
}
}
接着定义 Service Locator 类,该服务器类实现了一个服务 Cache 以及一个服务实例返回函数 getService。getService 函数会优先返回缓存的服务实例,如果没有,则通过 Initializer 创建。
// Locator class
class ServiceLocator {
private static Cache cache;
static
{
cache = new Cache();
}
public static Service getService(String name)
{
Service service = cache.getService(name);
if (service != null) {
return service;
}
InitialContext context = new InitialContext();
Service ServiceOne = (Service)context.lookup(name);
cache.addService(ServiceOne);
return ServiceOne;
}
}
上面提到的 Cache 类和 Initializer 类具体实现如下:
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);
}
}
}
// Checking the context
// for ServiceOne and ServiceTwo
class InitialContext {
public Object lookup(String name)
{
if (name.equalsIgnoreCase("ServiceOne")) {
System.out.println("Creating a new ServiceOne object");
return new ServiceOne();
}
else if (name.equalsIgnoreCase("ServiceTwo")) {
System.out.println("Creating a new ServiceTwo object");
return new ServiceTwo();
}
return null;
}
}
最后是 Client 类,测试代码效果:
// Client class
class ServiceConsumer {
public static void main(String[] args)
{
Service service = ServiceLocator.getService("ServiceOne");
service.execute();
service = ServiceLocator.getService("ServiceTwo");
service.execute();
service = ServiceLocator.getService("ServiceOne");
service.execute();
service = ServiceLocator.getService("ServiceTwo");
service.execute();
}
}
输出结果:
Creating a new ServiceOne object
Executing ServiceOne
Creating a new ServiceTwo object
Executing ServiceTwo
Returning cached ServiceOne object
Executing ServiceOne
Returning cached ServiceTwo object
Executing ServiceTwo
[1] 30分钟学会UML类图
[2] Baeldung: Service Locator Pattern
[3] GeeksforGeeks: Service Locator Pattern
标签:标识 rate href bae and 设计模式 consumer lookup print
原文地址:https://www.cnblogs.com/guangze/p/12537446.html