开发者

chain-of-responsibility handler with java generics

I'm using the Chain of Responsibility design-pattern in Java. The chain as a whole represents a request for objects of certain types. Each "Handler" in the chain is responsible to handle the requested units of 1 type. All requests are handled in essentially the same way so I tried making the "Handler"-cla开发者_C百科ss generic. So in the Handle-class I need a method like this (the handling itself is simplified because it would only obfuscate my problem):

public class Handler<T>{
   int required;
   Handler<?> next;

   public void handle(Object O){
      if(o instanceof T){
         required --;
      }else{
         next.handle(o);
      }
   }
}

The problem is that an instanceof like this is impossible. Because the type T isn't explicitly stored during run time (or that's what I understood during my research on the internet). So my question is: what is the best alternative?


Implement handlers using generics by using a constructor parameter to define the class the handler supports:

public class Handler<T> {
    private int required;
    private Handler<?> next;
    private Class<? extends T> c;

    public Handler(Class<? extends T> c) {
        this.c = c;
    }

    public void handle(Object o) {
        if (c.isInstance(o)) {
            required--;
        } else {
            next.handle(o);
        }
    }

    // ...
}    


It looks like you're not actually using a chain at all, unless you have some cases where both base and sub classes kick off events. If that unless part doesn't apply, you could do something like

Map<Class, Handler> handlers = //...initialize however

and in root handler:

public void handle(Object o) {
 handlers.get(o.getClass()).handle(o);
}


It doesn't make sense to have a handler using generics if you call handle on every object. Either you instantiate an handler for type like this:

public class Handler<T>{
   int required;
   Handler<?> next;

   public void handle(T O){
     ...
   }
}

or you define a abstract class handler and let the specific subclass to handle specific type or just pass the event to the chain. Also using

if( x.isInstance(o)) {...}

is really an antipattern and you can break OOP rules.


It would be ugly, but you could try this:

public abstract class Handler {
   int required;
   Handler next;

   public void handle(Object o){
      if(getMyClass().isInstance(o)){
         required --;
      }else{
         next.handle(o);
      }
   }

   protected abstract Class getMyClass();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜