5

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"-class 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?

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
Ingdas
  • 1,446
  • 1
  • 11
  • 21

4 Answers4

2

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

    // ...
}    
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
  • indeed.... it works for higher object level, but generics seems to default to `Object` in method signatures at compile time. Anyway. The second solution is removed, but the first solution still fits good in what you're trying to achieve – Yanick Rochon Nov 27 '10 at 15:43
  • this is closest to the solution I wanted to achieve, thanks. Now I can drop the generic part, because I can do everything with the Class as an argument – Ingdas Nov 27 '10 at 16:26
1

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();
}
thejh
  • 44,854
  • 16
  • 96
  • 107
  • Isn't `getClass` defined as `public` on `java.lang.Object`? It looks like you're trying to override it with a tighter access level. – Mike Samuel Nov 27 '10 at 16:17
  • And if you choose a different name, then `Class` should have a type parameter: `protected abstract Class extends T> getHandledValueType();` – Mike Samuel Nov 27 '10 at 16:18
  • @Mike Samuel: Corrected the `getClass()` issue, but the second thing just makes calling the code more complicated (and safer, yes). – thejh Nov 27 '10 at 16:23
  • I only raise it because you will get a compiler warning. Reasonable people can obviously disagree on what to do about warnings, but I would strongly suggest using type parameters in library code like this since it can potentially expose errors in multiple clients' code. – Mike Samuel Nov 28 '10 at 15:20
1

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);
}
Carl
  • 7,538
  • 1
  • 40
  • 64
1

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.

Uberto
  • 2,712
  • 3
  • 25
  • 27