RequestHandlerAPIAdapter

RequestHandlerAPIAdapter

假设我必须为Request期间发生的事件分配事件处理程序。该API公开了一个抽象类RequestHandlerAPIAdapter和一个接口RequestHandlerAPI来手动定义事件处理程序。 RequestHandlerAPIAdapter实现RequestHandlerAPI。该适配器仅存在,因此我可以执行以下操作:

client.setRequestHandler(new RequestHandlerAPIAdapter(Request request) {
    // override some or all inherited methods
});


请注意,适配器仅允许我处理我感兴趣的事件,而忽略其他事件。例如,如果我只想在发送请求时做某些事情,而不关心正在处理或响应到达时会发生什么,那么我只能为onRequestSent指定一个处理程序,而忽略onRequestProcessing onResponseArrived。这种设计模式足以满足大多数现实世界的需求。

现在假设我不知道我需要预先处理哪些事件,并且在创建onResponseArrived的匿名实例之后,我可能会决定将来是否应手动处理RequestHandlerAPIAdapter。将需要一个新的RequestHandlerAPIAdapter实例,我将不得不重复onRequestSent的定义。我知道这听起来很奇怪,也许开发人员永远不需要这种方式编写代码,但是我想知道在这种情况下是否存在防止代码重复的解决方法。

我想到的一个解决方案是创建一个实现RequestHandlerAPI或扩展RequestHandlerAPIAdapter的具体类,为每个继承的方法定义公共功能接口,为刚刚定义的所有功能接口定义私有字段,并添加方法setHandler对于请求期间可能发生的每个事件。

这是此类的代码:

public class RequestHandlerConcreteAdapter implements RequestHandlerAPI {
    private HandleOnRequestSent onRequestSent = null; // or may be an empty lambda expression that does nothing
    private HandleOnRequestProcessing onRequestProcessing = null;
    private HandleOnResponseRetrieved onResonseRetrieved = null;

    public interface HandleOnRequestSent {
        void onRequestSent (Request request);
    }

    public interface HandleOnRequestProcessing {
        void onRequestProcessing (Request request);
    }

    public interface HandleOnResponseRetrived {
        void onResponseRetrived (Request request, Response response);
    }

    @Override
    public void onRequestSent (Request request) {
        onRequestSent.onRequestSent(request);
    }

    public RequestHandlerConcreteAdapter setHandler (HanldeOnRequestSent h) {
        onRequestSent = h;
        return this; // allows us to chain methods
    }

    @Override
    public void onRequestProcessing (Request request) {
        onRequestProcessing.onrequestProcessing(request);
    }

    // overloaded
    public RequestHandlerConcreteAdapter setHandler (HanldeOnRequestProcessing h) {
        onRequestProcessing = h;
        return this; // allows us to chain methods
    }

    // and a pair to handle onResponseRetrived
}


这使我可以执行以下操作:

RequestHandlerConcreteAdapter adapter = new RequestHandlerConcreteAdapter ();

adapter.setHandler ( (RequestHandlerConcreteAdapter.HandleOnRequestSent) (request) -> {
    //do something here
});

client.setRequestHandler (adapter);

// and at a later time
adapter.setHandler ( (RequestHandlerConcreteAdapter.HanldeOnResponseRetrieved) (request, response) -> {
    // do something here
});

client.setRequestHandler (adapter);


我会寻找一切可能的方法来避免做上面的事情,因为这种解决方案对我来说太脏了。另外,我想知道我所做的是否是Java中的已知设计模式。

综上所述,我想知道是否有一种更干净的方法来实现上面的代码正在执行的操作,如果不能,您是否可以建议对代码进行一些改进以使其看起来不太脏?

最佳答案

在我看来,您只是在寻找Decorator pattern。实现一个由RequestHandlerAPIDecorator实例创建的RequestHandlerAPI,并将所有onRequest<DoSomething>调用委托给它。要覆盖某些操作,请为RequestHandlerAPIDecorator子类化并覆盖方法之一。

大致情况:

 public abstract class RequestHandlerAPIDecorator implements RequestHandlerAPI {

     protected final RequestHandlerAPI delegate;

     public RequestHandlerAPIDecorator(RequestHandlerAPI delegate) {
         this.delegate = Objects.requireNonNull(delegate, "delegate must not be null.");
     }

     public void onRequestProcessing(Request request) {
         delegate.onRequestProcessing(request);
     }

     // Etc. You IDE will happily generate delegate methods for you.
 }


现在,如果要覆盖,对某些现有的RequestHandlerAPI说“请求发送”处理,只需执行以下操作:

 RequestHandlerAPI decoratedRequestHandler =
     new RequestHandlerAPIDecorator(existingRequestHandler) {
         public void onRequestSent(Request request) {
             System.out.println("Request is sent.");
         }
     });


您可以从头开始执行处理,也可以执行其他操作,然后再调用delegate.onRequestSent(request)

09-29 22:18