Open java principle for multiple services

Say I would like to define an interface that represents a call to a remote service.

Both services have different requests and responses.

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

Now let's look at the implementation

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
  }
}

In another class, depending on some value in the request, I create an instance of either ServiceA, orServiceB

I have questions regarding the above:

Is using a universal interface ExecutesService<T,S>good if you want to provide subclasses that require different Requestand Response.

What is the best way to do it higher?

+4
source share
4 answers

, , , executeFifthService() ServiceA ServiceB .. .

A, B .., , , .

:

ExecutesService:

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.

:

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 ( FirstService SecondService ..) ServiceA , supportedListOfServices, UnsupportedOperationException.

, - ( , executeFifthService() ServiceA, B ..)), FifthService .

+1

, . , , , , .

, , . :

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

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

public interface ExecutesServiceB<T, S> extends ExecutesService {
    T executeFirstService(S obj);
}

, , public .

, .

+1

, , . , ?
, API , ( ) .

, API , , , .

(ExecutesService) , .

, , , , :

public interface ExecutesService<T,S>

, .

, :

ExecutesService<String, Integer> myVar = new ExecutesService<>();

:

ExecutesService<Boolean, String> otherVar

myVar = otherVar.

, .
, , .
, , .

Using classic inheritance (without generics), you would probably introduce very clear interfaces.

+1
source

I would suggest you create two different interfaces, each of which processes its own requests and response types. Of course, you can develop an implementation with one common interface that processes all the logic, but this can make the code more complicated and dirty from my point of view. considers

+1
source

Source: https://habr.com/ru/post/1674824/


All Articles