java多服务开闭原则

标签 java generics inheritance interface

假设我想定义一个表示对远程服务的调用的接口(interface)。

两种服务都有不同的请求和响应

public interface ExecutesService<T,S> {
    public T executeFirstService(S obj);
    public T executeSecondService(S obj);
    public T executeThirdService(S obj);
    public T executeFourthService(S obj);
}

现在,让我们看看实现

public class ServiceA implements ExecutesService<Response1,Request1>
{
  public Response1 executeFirstService(Request1 obj)
  {
    //This service call should not be executed by this class
    throw new UnsupportedOperationException("This method should not be called for this class");
  }

  public Response1 executeSecondService(Request1 obj)
  {
    //execute some service
  }

   public Response1 executeThirdService(Request1 obj)
  {
    //execute some service
  }

   public Response1 executeFourthService(Request1 obj)
  {
    //execute some service
  }
}


public class ServiceB implements ExecutesService<Response2,Request2>
{

 public Response1 executeFirstService(Request1 obj)
  {
    //execute some service
  }

  public Response1 executeSecondService(Request1 obj)
  {
      //This service call should not be executed by this class
    throw new UnsupportedOperationException("This method should not be called for this class");
  }

   public Response1 executeThirdService(Request1 obj)
  {
      //This service call should not be executed by this class
    throw new UnsupportedOperationException("This method should not be called for this class");
  }

   public Response1 executeFourthService(Request1 obj)
  {
    //execute some service
  }
}

在另一个类中,根据请求中的某些值,我正在创建 ServiceA 的实例或 ServiceB

我对上述问题有疑问:

是使用通用接口(interface)ExecutesService<T,S>在您想提供需要不同 Request 的子类的情况下很好和 Response .

我怎样才能做得更好?

最佳答案

基本上,您的当前设计违反了open closed principle 即,如果您想添加 executeFifthService() 怎么办? ServiceA 的方法和 ServiceB等.. 类。

更新所有服务 A、B 等并不是一个好主意。简而言之,类应该对扩展开放但对修改关闭

相反,您可以引用以下方法:

ExecutesService 接口(interface):

public interface ExecutesService<T,S> {
    public T executeService(S obj);
}

ServiceA 类:

public class ServiceA implements ExecutesService<Response1,Request1> {

    List<Class> supportedListOfServices = new ArrayList<>();
    //load list of classnames supported by ServiceA during startup from properties

    public Response1 executeService(Request1 request1, Service service) {
        if(!list.contains(Service.class)) {
           throw new UnsupportedOperationException("This method should 
                      not be called for this class");
        } else {
            return service.execute(request1);
        }
    }
}

同样,你可以实现ServiceB

服务接口(interface):

public interface Service<T,S> {
    public T execute(S s);
}

FirstService类:

public class FirstService implements Service<Request1,Response1> {
    public Response1 execute(Request1 req);
}

同样,你需要实现SecondService , ThirdService等等。

因此,在这种方法中,您基本上传递了 Service (要实际调用,它可能是 FirstServiceSecondService 等。)在运行时和 ServiceA验证它是否在 supportedListOfServices 中, 如果没有抛出 UnsupportedOperationException .

这里的重点是您不需要更新任何现有服务来添加新功能(不像您需要在 executeFifthService()ServiceA 等中添加 B 的设计。) ,而您需要再添加一个名为 FifthService 的类并通过它。

关于java多服务开闭原则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410500/

相关文章:

java - 在 Java 中处理具体类型时 instanceof 的替代方法

Java:通用方法 - 将元素添加到 vararg

css 继承上面的样式

c++ - 派生异常不继承构造函数

css - 删除继承的 css 3d 转换

Java正则表达式模式匹配

java - WebLogic:URL 映射不起作用

java - 如何使用 CAPTURE 绑定(bind)创建 AST?

java - 通过 BeanFactory.getBean(Class<T>) 检索通用 bean

java - 使具有相同删除二进制文件的通用返回类型兼容吗?