0
我有一個抽象的超級服務應該執行一些共同的邏輯。 幾個服務實現這個超級服務。我根據條件選擇了ServiceImpl,並希望將其分配給抽象類型,以便稍後運行公共邏輯。如何分組不同的服務來執行基本邏輯?
但我不會讓我寫類型安全代碼,所以設計中可能有些東西是錯誤的。我如何改進以下內容?
//superservice
abstract class AbstractService<T extends BaseResponse> {
public void run() {
//execute some common logic
}
}
//implementations
class FirstService extends AbstractService<FirstResponse extends BaseResponse> {
}
class SecondService extends AbstractService<SecondResponse extends BaseResponse> {
}
用法:
//this works, but claims to have missing type argument, not being typesafe
AbstractService myservice = condition ? new FirstService() : new SecondService();
myservice.run();
----------------
//this will not compile with "type missmatch" hint
AbstractService<BaseResponse> myservice = condition ? new FirstService() : new SecondService();
myservice.run();
你打算怎麼使用反應?也許在這裏不需要使用泛型。 – bmanvelyan