Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 627
Ingdas Avatar asked Nov 27 '10 14:11

Ingdas


People also ask

What is Chain of Responsibility in Java?

Chain of Responsibility is behavioral design pattern that allows passing request along the chain of potential handlers until one of them handles request. The pattern allows multiple objects to handle the request without coupling sender class to the concrete classes of the receivers.

In what scenarios can Chain of Responsibility be used?

Where and When Chain of Responsibility pattern is applicable : When you want to decouple a request's sender and receiver. Multiple objects, determined at runtime, are candidates to handle a request. When you don't want to specify handlers explicitly in your code.

How do generics work internally?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.

Which are the three participants of the Chain of Responsibility design pattern?

Chain of Responsibility, Command, Mediator and Observer address various ways of connecting senders and receivers of requests: Chain of Responsibility passes a request sequentially along a dynamic chain of potential receivers until one of them handles it.


2 Answers

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

    // ...
}    
like image 176
Yanick Rochon Avatar answered Sep 24 '22 14:09

Yanick Rochon


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);
}
like image 21
Carl Avatar answered Sep 24 '22 14:09

Carl