2016-05-27 8 views
2

私はHandlerインターフェースを作成しようとしています。このインターフェースは、そのタイプに基づいてさまざまなタイプのイベントを処理できます。ここでJava未チェックの生タイプへの呼び出し

Unchecked call to 'handle(T)' as a member of raw type 'Handler'

私のクラスである:私は次の警告とのトラブルを抱えています。

public interface Handler<T> { 
    void handle(T event); } 

public class IntegerHandler implements Handler<Integer> { 
    @Override 
    public void handle(Integer event) { 
     System.out.println("Integer: " + event); 
    } 
} 

public class ObjectHandler implements Handler<Object> { 
    @Override 
    public void handle(Object event) { 
     System.out.println("Object: " + event); 
    } 
} 

public class StringHandler implements Handler<String> { 
    @Override 
    public void handle(String event) { 
     System.out.println("String: " + event); 
    } 
} 

public class TestHandlers { 

    public static void main(String[] args) { 

     String a = "hello"; 
     Integer b = 12; 
     Long c = 23L; 

     dispatch(a).handle(a); 
     dispatch(b).handle(b); 
     dispatch(c).handle(c); 
    } 

    private static Handler dispatch(Object o) { 
     if (o instanceof String) { 
      return new StringHandler(); 
     } else if (o instanceof Integer) { 
      return new IntegerHandler(); 
     } else { 
      return new ObjectHandler(); 
     } 
    } 
} 

出力が正しいよう:

String: hello 
Integer: 12 
Object: 23 

私は問題が私のdispatch方法はHandlerの未チェックのバージョンを返しているということだと思います。

これを行うには、適切な方法が何であるか不明です。図示していない

+0

理由だけではなく、 ''から '' 'handle'''呼び出します'ディスパッチ' 'メソッド?つまり、 '' '' new StringHandler.handle((String)o); '' ' –

+0

コードはすでに動作しているので、コードを修正しようとしていません。私はデザインの質問をしようとしています。 – scabbage

+3

'dispatch(a).handle(b)'を試しましたか? [生の型](http://stackoverflow.com/q/2770321/2891664)を返すと、 'Handler'は基本的にあなたのコードを非ジェネリックにします。あなたが望むものは 'static Handler dispatch(T o)'ですが、実際にはタイプセーフではありません。少なくとも私は 'static Handler ディスパッチ(クラスc)'への変更を提案します。 – Radiodef

答えて

0

Generic Factory With Unknown Implementation Classesにクレジット)ファクトリを作成します

public class HandlerFactory<H extends Handler> { 

final Class<H> handlerClass; 

protected HandlerFactory(final Class<H> clazz) { 
    handlerClass = clazz; 
} 

protected H create() throws InstantiationException, IllegalAccessException { 
    H handler = handlerClass.newInstance(); 
    return handler; 
} 

public static <H extends Handler> HandlerFactory<H> createFactory(final Class<H> clazz) throws InstantiationException, IllegalAccessException { 
    return new HandlerFactory(clazz); 
} 

public static Handler dispatch(Object obj) throws InstantiationException, IllegalAccessException { 
    Class c; 

    if (obj instanceof String) { 
     c = StringHandler.class; 
    } else if (obj instanceof Integer) { 
     c = IntegerHandler.class; 
    } else { 
     c = ObjectHandler.class; 
    } 
    HandlerFactory factory = HandlerFactory.createFactory(c); 
    return factory.create(); 
} 

}

例外ハンドラ:

... 
String a = "hello"; 
Integer b = 12; 
Long c = 23L; 

HandlerFactory.dispatch(a).handle(a); 
HandlerFactory.dispatch(b).handle(b); 
HandlerFactory.dispatch(c).handle(c); 
関連する問題