私の質問は言葉で説明するのは簡単ではありませんが、幸いなことにそれを示すことはそれほど難しくありません。だから、私と一緒に耐えてください:
public interface Command<R>
{
public R execute();//parameter R is the type of object that will be returned as the result of the execution of this command
}
public abstract class BasicCommand<R> implements Command<R>
{
}
public interface CommandProcessor<C extends Command<?>>
{
public <R> R process(C<R> command);//this is my question... it's illegal to do, but you understand the idea behind it, right?
}
//constrain BasicCommandProcessor to commands that subclass BasicCommand
public class BasicCommandProcessor<C extends BasicCommand<?>> implements CommandProcessor<C>
{
//here, only subclasses of BasicCommand should be allowed as arguments but these
//BasicCommand object should be parameterized by R, like so: BasicCommand<R>
//so the method signature should really be
// public <R> R process(BasicCommand<R> command)
//which would break the inheritance if the interface's method signature was instead:
// public <R> R process(Command<R> command);
//I really hope this fully illustrates my conundrum
public <R> R process(C<R> command)
{
return command.execute();
}
}
public class CommandContext
{
public static void main(String... args)
{
BasicCommandProcessor<BasicCommand<?>> bcp = new BasicCommandProcessor<BasicCommand<?>>();
String textResult = bcp.execute(new BasicCommand<String>()
{
public String execute()
{
return "result";
}
});
Long numericResult = bcp.execute(new BasicCommand<Long>()
{
public Long execute()
{
return 123L;
}
});
}
}
基本的に、私は、一般的な「プロセス」メソッドがCommandオブジェクトの一般的なパラメーターのタイプを指示することを望みます。目標は、CommandProcessorの異なる実装を、Commandインターフェースを実装する特定のクラスに制限できると同時に、CommandProcessorインターフェースを実装する任意のクラスのプロセスメソッドを呼び出して、指定されたタイプのオブジェクトを返せるようにすることです。パラメータ化されたCommandオブジェクト。私の説明が十分に明確かどうかはわかりませんので、さらに説明が必要な場合はお知らせください。たぶん、問題は「これで実現できるだろうか?」答えが「いいえ」の場合、最善の回避策は何ですか(私は自分でカップルを考えましたが、いくつかの新鮮なアイデアが欲しいです)
残念ながら、これはできません。あなたがしたいので、CommandProcessor
インターフェースはで定義されるようにCommand
、あなたのimplemnetationは、あらゆる種類の取るために準備しなければならないCommand
のインスタンスを-ジェネリックは、これを制限することはできませんBasicCommand
、それは、その後、できれば- BasicCommandProcessor
サブクラスが実装しないとCommandProcessor
のインターフェイスを。
または、別の角度から見ると、CommandProcessor
インターフェースが与えられているため、ジェネリックスがBasicCommand
インスタンスでのみ呼び出されるようにすることはできません。これを行うには、難しさを知る必要があり、多態性とインターフェースのポイントに反することになります。
コマンドの結果はパラメーター化できますが、コマンドの具体的なクラスはパラメーター化できません。
public interface Command<R>
{
public R execute();//parameter R is the type of object that will be returned as the result of the execution of this command
}
public abstract class BasicCommand<R> implements Command<R>
{
}
public interface CommandProcessor
{
public <R> R process(Command<R> command);
}
public class BasicCommandProcessor implements CommandProcessor
{
public <R> R processBasicCommand(BasicCommand<R> command)
{
return command.execute();
}
public <R> R process(Command<R> command)
{
return processBasicCommand((BasicCommand<R>)command);
}
}
最も簡単な方法は、必要な特定の型を受け入れるメソッドを提供し、それをジェネリックメソッドで呼び出すことです。(上記のBasicCommandProcessorを参照してください。)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加