ITPub博客

首页 > IT职业 > IT职场 > Service Locator 模式

Service Locator 模式

IT职场 作者:lotus_ruan 时间:2021-09-09 09:14:05 0 删除 编辑

什么是Service Locator 模式?

服务定位模式(Service Locator Pattern)是一种软件开发中的设计模式,通过应用强大的抽象层,可对涉及尝试获取一个服务的过程进行封装。该模式使用一个称为"Service Locator"的中心注册表来处理请求并返回处理特定任务所需的必要信息。

场景描述

某类ClassA依赖于服务ServiceA和服务ServiceB,服务的具体类型需在编译时指定。

图片描述

这种条件下有以下缺点:

  • 尝试替换或更新依赖项,必须更改类的源代码并且重新编译。

  • 依赖项的具体实现必须在编译时可用。

  • 测试该类非常困难,因为类对依赖项有直接的引用,则依赖项不能使用Stub或Mock对象替换。

  • 该类包含用于创建、定位和管理依赖项的重复代码。

设计目标

使用 Service Locator Pattern 来达成以下目标:

  • 把类与依赖项解耦,从而使这些依赖项可被替换或者更新。

  • 类在编译时并不知道依赖项的具体实现。

  • 类的隔离性和可测试性非常好。

  • 类无需负责依赖项的创建、定位和管理逻辑。

  • 通过将应用程序分解为松耦合的模块,达成模块间的无依赖开发、测试、版本控制和部署。

解决方案

创建一个 Service Locator,其包含各服务的引用,并且封装了定位服务的逻辑。在类中使用 Service Locator 来获取所需服务的实例。

图片描述

Service Locator 模式并不描述如何实例化服务,其描述了一种注册和定位服务的方式。通常情况下,Service Locator 模式与工厂模式(Factory Pattern)和依赖注入模式(Dependency Injection Pattern)等结合使用。

服务定位器应该能够在不知道抽象类的具体类型的情况下定位到服务。例如,它可能会使用字符串或服务接口类型来影射服务,这允许在无需修改类的条件下替换依赖项的具体实现。

实现细节

通常 ServiceLocator 类提供 IServiceLocator 接口的实现单例,并负责管理该实例的创建和访问。ServiceLocator 类提供 IServiceLocator 接口的默认实现,例如 ActivatingServiceLocator 类,可以同时创建和定位服务。

注意事项

在使用 Service Locator 模式之前,请考虑以下几点:

  • 有很多程序中的元素需要管理。

  • 在使用之前必须编写额外的代码将服务的引用添加到服务定位器。

  • 类将对服务定位器有依赖关系。

  • 源代码变的更加复杂和难以理解。

  • 可以使用配置数据来定义运行时的关系。

  • 必须提供服务的实现。因为服务定位器模式将服务消费者与服务提供者解耦,它可能需要提供额外的逻辑。这种逻辑将保证在服务消费者尝试定位服务之前,服务提供者已被安装和注册。

相关模式

  • 依赖注入(Dependency Injection)。这种模式解决了与 Service Locator 模式相同的问题,但它使用不同的方法。

  • 控制反转(Inversion of Control)。Service Locator 模式是这种模式的特殊版本。它将应用程序的传统控制流程反转。它用被调用对象来代替控制过程的调用方。

参考信息

  • Inversion of Control and the Dependency Injection pattern on Martin Fowler's Web site

  • Service Locator on MSDN

代码示例

Service Locator 的简单实现,使用静态类实现,未使用Singleton设计,仅作Mapping影射。

图片描述

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;

namespace Infrastructure
{
  /// 
  /// 服务定位器
  /// 
  public static class ServiceLocator
  {
    #region Fields

    private static readonly Dictionary _mapping 
    = new Dictionary();
    private static readonly Dictionary _resources 
    = new Dictionary();
    private static object _operationLock = new object();

    #endregion

    #region Add

    /// 
    /// 添加注册资源
    /// 
    /// 资源类型
    /// 资源实例
    public static void Add(object instance)
        where TClass : class
    {
      Add(typeof(TClass), instance);
    }

    /// 
    /// 添加注册资源
    /// 
    /// 资源类型
    /// 资源实例
    public static void Add(Type typeOfInstance, object instance)
    {
      if (typeOfInstance == null)
        throw new ArgumentNullException("typeOfInstance");
      if (instance == null)
        throw new ArgumentNullException("instance");

      if (!(typeOfInstance.IsInstanceOfType(instance)))
      {
        throw new InvalidCastException(
            string.Format(CultureInfo.InvariantCulture,
            "Resource does not implement supplied interface: {0}", 
       typeOfInstance.FullName));
      }

      lock (_operationLock)
      {
        if (_resources.ContainsKey(typeOfInstance))
        {
          throw new ArgumentException(
              string.Format(CultureInfo.InvariantCulture, 
        "Resource is already existing : {0}", typeOfInstance.FullName));
        }
        _resources[typeOfInstance] = instance;
      }
    }

    #endregion

    #region Get

    /// 
    /// 查找指定类型的资源实例
    /// 
    /// 资源类型
    /// 资源实例
    public static TClass Get()
        where TClass : class
    {
      return Get(typeof(TClass)) as TClass;
    }

    /// 
    /// 查找指定类型的资源实例
    /// 
    /// The type of instance.
    /// 资源实例
    public static object Get(Type typeOfInstance)
    {
      if (typeOfInstance == null)
        throw new ArgumentNullException("typeOfInstance");

      object resource;

      lock (_operationLock)
      {
        if (!_resources.TryGetValue(typeOfInstance, out resource))
        {
          throw new ResourceNotFoundException(typeOfInstance.FullName);
        }
      }

      if (resource == null)
      {
        throw new ResourceNotInstantiatedException(typeOfInstance.FullName);
      }

      return resource;
    }

    /// 
    /// 尝试查找指定类型的资源实例
    /// 
    /// 资源类型
    /// 资源实例
    /// 是否存在指定资源类型的资源实例
    public static bool TryGet(out TClass resource)
        where TClass : class
    {
      bool isFound = false;

      resource = null;
      object target;

      lock (_operationLock)
      {
        if (_resources.TryGetValue(typeof(TClass), out target))
        {
          resource = target as TClass;
          isFound = true;
        }
      }

      return isFound;
    }

    #endregion

    #region Register

    /// 
    /// 注册类型
    /// 
    /// 实体类型,类型限制为有公共无参构造函数
    public static void RegisterType()
      where TClass : class, new()
    {
      lock (_operationLock)
      {
        _mapping[typeof(TClass)] = typeof(TClass);
      }
    }

    /// 
    /// 注册类型
    /// 
    /// 资源类型
    /// 实体类型,类型限制为有公共无参构造函数
    public static void RegisterType()
      where TFrom : class
      where TTo : TFrom, new()
    {
      lock (_operationLock)
      {
        _mapping[typeof(TFrom)] = typeof(TTo);
        _mapping[typeof(TTo)] = typeof(TTo);
      }
    }

    /// 
    /// 是否已注册此类型
    /// 
    /// 资源类型
    /// 是否已注册此类型
    public static bool IsRegistered()
    {
      lock (_operationLock)
      {
        return _mapping.ContainsKey(typeof(TClass));
      }
    }

    #endregion

    #region Resolve

    /// 
    /// 获取类型实例
    /// 
    /// 资源类型
    /// 类型实例
    public static TClass Resolve()
      where TClass : class
    {
      TClass resource = default(TClass);

      bool existing = TryGet(out resource);
      if (!existing)
      {
        ConstructorInfo constructor = null;

        lock (_operationLock)
        {
          if (!_mapping.ContainsKey(typeof(TClass)))
          {
            throw new ResourceNotResolvedException(
              string.Format(CultureInfo.InvariantCulture, 
        "Cannot find the target type : {0}", typeof(TClass).FullName));
          }

          Type concrete = _mapping[typeof(TClass)];
          constructor = concrete.GetConstructor(
        BindingFlags.Instance | BindingFlags.Public, null, new Type[0], null);
          if (constructor == null)
          {
            throw new ResourceNotResolvedException(
              string.Format(CultureInfo.InvariantCulture, 
        "Public constructor is missing for type : {0}", typeof(TClass).FullName));
          }
        }

        Add((TClass)constructor.Invoke(null));
      }

      return Get();
    }

    #endregion

    #region Remove

    /// 
    /// 移除指定类型的资源实例
    /// 
    /// 资源类型
    public static void Remove()
    {
      Teardown(typeof(TClass));
    }

    /// 
    /// 移除指定类型的资源实例
    /// 
    /// 资源类型
    public static void Remove(Type typeOfInstance)
    {
      if (typeOfInstance == null)
        throw new ArgumentNullException("typeOfInstance");

      lock (_operationLock)
      {
        _resources.Remove(typeOfInstance);
      }
    }

    #endregion

    #region Teardown

    /// 
    /// 拆除指定类型的资源实例及注册映射类型
    /// 
    /// 资源类型
    public static void Teardown()
    {
      Teardown(typeof(TClass));
    }

    /// 
    /// 拆除指定类型的资源实例及注册映射类型
    /// 
    /// 资源类型
    public static void Teardown(Type typeOfInstance)
    {
      if (typeOfInstance == null)
        throw new ArgumentNullException("typeOfInstance");

      lock (_operationLock)
      {
        _resources.Remove(typeOfInstance);
        _mapping.Remove(typeOfInstance);
      }
    }

    #endregion

    #region Clear

    /// 
    /// 移除所有资源
    /// 
    public static void Clear()
    {
      lock (_operationLock)
      {
        _resources.Clear();
        _mapping.Clear();
      }
    }

    #endregion
  }
}

Service Locator 测试代码


using System;
using Infrastructure;

namespace ServiceLocatorTest
{
  class Program
  {
    interface IServiceA
    {
      string GetData();
    }

    class ServiceA : IServiceA
    {
      public string GetData()
      {
        return "This data is from ServiceA";
      }
    }

    static void Main(string[] args)
    {
      ServiceLocator.RegisterType();
      IServiceA serviceA = ServiceLocator.Resolve();
      string data = serviceA.GetData();
      Console.WriteLine(data);
      Console.ReadKey();
    }
  }
}

图片描述

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1762/viewspace-2800869/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2015-01-10

  • 博文量
    79
  • 访问量
    17976