多态性,如何避免类型转换?

时间:2014-10-01 22:38:47

标签: java oop casting polymorphism

我很抱歉这个长期的问题,但请耐心等待,我尽量让我的问题变得可以理解。如果您认为可以更简洁,请随时编辑它。

我有一个客户端 - 服务器系统,客户端向服务器发送不同类型的请求,并根据请求获取响应。

客户端系统中的代码是:

 int requestTypeA() {
      Request request = new Request(TypeA);
      Response response = request.execute();
      // response for request of TypeA contains a int
      return response.getIntResponse();
 }

 String requestTypeB() {
      Request request = new Request(TypeB);
      Response response = request.execute();
      // response for request of TypeB contains a String
      return response.getStringResponse();
 }

要使上述代码正常运行,Request类为:

 class Request {
       Type type;
       Request(Type type) {
           this.type = type;
        }

        Response execute() {
              if (type == TypeA) { 
                  // do stuff
                  return new Response(someInt);
              }
              else if (type == TypeB) {
                  // do stuff
                  return new Response("someString");
              }
              else if ...
        }
 }

Response是这样的:

 class Response {
      int someInt;
      String someString;

      Response(int someInt) {
          this.someInt = someInt;
      }

      Response(String someString) {
          this.someString = someString;
      }

      int getIntResponse() {
           return someInt;
      }

      String getStringResponse() {
          return someString;
      }
 }

上述解决方案有两个问题:

  1. execute方法将充满ifelse if块。
  2. 可能是当返回错误的响应时,例如someString未初始化的地方,例如它与A类请求的响应相混淆。
  3. 关于第一个问题我想出的解决方案是使用多态。所以有一个父类Request,并且对于每种类型的请求都有一个Request的子类,所以有一个RequestTypeARequestTypeB。所有类都覆盖execute方法。

    关于2.问题我对如何解决它只有一个可能的想法:与Request类似,根据响应创建Response的子类并具有类似的内容。

     interface Response {
     }
    
     class ResponseTypeA {
         ResponseTypeA(int i) { ... }
         int getIntResponse() { ... }
     }
    
     class ResponseTypeB {
         ResponseTypeB(String s) { ... verify s is valid ... }
         String getStringResponse() { ... }
     }
    

    现在我可以肯定,如果类型为ResponseTypeB的响应,它将包含有效的字符串。我可以按如下方式编写客户端代码:

    String requestTypeB() {
        Request request = new Request(TypeB);
        ResponseTypeB response = (ResponseTypeB) request.execute();
        return response.getStringResponse();
     }
    

    现在我不得不输入转换类型execute

    我的主要问题是:在上述情况下,有没有办法避免类型转换?或者,如果您知道上述问题的更好解决方案(设计模式?)?

9 个答案:

答案 0 :(得分:7)

尝试将请求与响应分开是徒劳的。它们由API绑定在一起 - R r = f(Q)

您有RequestA返回intRequestB,返回String。你可以清楚地做类似的事情:

class Conversation<Q,R> {
    R request (Q q, Class<R> rType) {
        // Send the query (Q) and get a response R
    }
}

class ConversationA extends Conversation<RequestA, Integer> {

}
class ConversationB extends Conversation<RequestB, String> {

}

更加充实的版本可能类似于:

public class Test {

    // Extend this to magically get a JSON-Like toString.
    public static interface JSONObject {

        public String asJSON();
    }

    class RequestA implements JSONObject {

        @Override
        public String asJSON() {
            return "RequestA {}";
        }
    }

    class RequestB implements JSONObject {

        @Override
        public String asJSON() {
            return "RequestB {}";
        }
    }

    static class Conversation<Q extends JSONObject, R> {

        // Parser factory.
        private static final JsonFactory factory = new JsonFactory();

        // General query of the website. Takes an object of type Q and returns one of class R.
        public R query(String urlBase, String op, Q q, Class<R> r) throws IOException {
            // Prepare the post.
            HttpPost postRequest = new HttpPost(urlBase + op);
            // Get it all into a JSON string.
            StringEntity input = new StringEntity(q.asJSON());
            input.setContentType("application/json");
            postRequest.setEntity(input);
            // Post it and wait.
            return requestResponse(postRequest, r);
        }

        private <R> R requestResponse(HttpRequestBase request, Class<R> r) throws IOException {
            // Start a conversation.
            CloseableHttpClient httpclient = HttpClients.createDefault();
            CloseableHttpResponse response = httpclient.execute(request);
            // Get the reply.
            return readResponse(response, r);
        }

        private <R> R readResponse(CloseableHttpResponse response, Class<R> r) throws IOException {
            // What was read.
            R red = null;
            try {
                // What happened?
                if (response.getStatusLine().getStatusCode() == 200) {
                    // Roll out the results
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        // Always make sure the content is closed.
                        try (InputStream content = entity.getContent()) {
                            red = parseAs(content, r);
                        }
                    }
                } else {
                    // The finally below will clean up.
                    throw new IOException("HTTP Response: " + response.getStatusLine().getStatusCode());
                }
            } finally {
                // Always close the response.
                response.close();
            }

            return red;
        }

        private <R> R parseAs(InputStream content, Class<R> r) throws IOException {
            JsonParser rsp;
            // Roll it directly from the response stream.
            rsp = factory.createJsonParser(content);
            // Bring back the response.
            return rsp.readValueAs(r);
        }
    }

    static class ConversationA extends Conversation<RequestA, Integer> {

    }

    static class ConversationB extends Conversation<RequestB, String> {

    }

    public void test() throws IOException {
        Integer a = new ConversationA().query("http://host/api", "JSON", new RequestA(), Integer.class);
        String b = new ConversationB().query("http://host/api", "JSON", new RequestB(), String.class);
    }

    public static void main(String args[]) {
        try {
            new Test().test();
        } catch (Throwable t) {
            t.printStackTrace(System.err);
        }
    }
}

这是从JSON和Apache HttpClient的实际使用中得出的 - 但是,它可能无法发布,因为我已经删除了大多数错误处理和重试机制以简化。这里主要是为了证明建议机制的使用。

请注意,虽然此代码中没有强制转换(根据问题的要求),但rsp.readValueAs(r)中可能会出现无法使用JSON进行投射的情况。

答案 1 :(得分:4)

基于类型的每个开关(或者if / else if / else链)是sign for a bad OO design

正如OldCurmudgeon所说:每个请求都受其响应的约束 - 请求和响应是一对。因此,我会完全按照您在文中提出的建议行事,但未在您的代码中实施:

关于第一个问题,我提出的解决方案是使用多态。所以有一个父类Request并且对于每种类型的请求都有一个Request的子类,所以有一个RequestTypeA和RequestTypeB。所有类都覆盖了execute方法。 所以基类看起来像:

/**
 * Abstract class Request forms the base class for all your requests.
 * Note that the implementation of execute() is missing.
 */
interface Request {
        public Response execute();
}

/**
 * Response-Interface just to have a common base class.
 */
interface Response {
}

请注意,我将Request从具体类更改为接口。 A的具体实现(covariant return types我避免了铸造)看起来像:

/**
 * Concrete request of type A.
 */
class RequestTypeA {
    /** all fields typically for request A. */
    private int i;

    /**
     * ctor, initializes all request fields.
     */
    public RequestTypeA(int i) {
        this.i = i;
    }

    /**
     * Provide the exact response type. A feature Java 5 introduced is covariant return types, which permits an overriding method to return a more specialized type than the overriden method. 
     */
    public ResponseTypeA execute()
    {
        // Your implementation here
        // you have to return a ResponseTypeA
    }
}

class ResponseTypeA implements Response {
    int getResponse() {
        // Your implementation here
    }
}

B的具体实现:

/**
 * Concrete request of type B.
 */
class RequestTypeB {
    /** all fields typically for request B. */
    private String s;

    /**
     * ctor, initializes all request fields.
     */
    public RequestTypeB(String s) {
        this.s = s;
    }

    /**
     * Provide the exact response type. A feature Java 5 introduced is covariant return types, which permits an overriding method to return a more specialized type than the overriden method. 
     */
    public ResponseTypeB execute()
    {
        // Your implementation here
        // you have to return a ResponseTypeB
    }
}

class ResponseTypeB implements Response {
    String getResponse() {
        // Your implementation here
    }
}

此设计可确保:

  • 每个Response都绑定到其Request,因为请求是获取响应的唯一方法
  • 您可以通过其公共接口访问请求和响应(如果您想共享功能,则可以创建一个抽象类)。
  • 每个请求和响应都可以拥有它的特定输入和输出参数(不止一次)
  • 您可以以类型安全的方式访问参数

用法示例:

    RequestTypeA reqA = new RequestTypeA(5);
    ResponseType resA = regA.execute();
    int result = resA.getResponse();

带有仿制药的解决方案(由OldCurmudgeon提供)也很好。在以下情况下使用所有请求/响应对的手动实现而不是泛型:

  • 每个请求/响应都有不同的参数(而不仅仅是一个)
  • 您希望使用纯数据类型而不是其盒装变体
  • 发送/检索的代码不是那么统一,只有专业化的数据类型处理才不同。

Groovy中的玩具实现(类固醇上的Java)查询Internet Chuck Norris Database

abstract class Request {
        public abstract Response execute();
        protected String fetch(String url) { new URL("http://api.icndb.com/jokes/$url").getText() }
}

interface Response {}

class RandomRequest extends Request {
        public CommonResponse execute() {
            new CommonResponse(result: fetch('random/'))
        }
}

class SpecificRequest extends Request {
        private int number;

        public CommonResponse execute() {
            new CommonResponse(result: fetch("$number"))
        }
}

class CommonResponse implements Response {
    private String result

    String getJoke() {
        def slurper = new groovy.json.JsonSlurper()
        slurper.parseText(result).value.joke
    }
}


println new RandomRequest().execute().joke
println new SpecificRequest(number: 21).execute().joke

答案 2 :(得分:1)

其他答案与泛型一致,但由于需要额外的类和响应类型的冗余声明,它们太复杂了。

这可能很简单:

    Response<Integer> a = new RequestA().execute();
    int resultA = a.getResult();

甚至

    String resultB = new RequestB().execute().getResult();

您不需要任何投射,因此它不会引发ClassCastException但会编译错误,因为没有泛型。

其他例子:

    AbstractRequest<Integer> requestC = new RequestC(); 
    Integer resultC = requestC.execute().getResult();

    // The only use case where you need casting, is when you create 
    // a response type hierarchy.
    AbstractRequest<? extends MyBaseClass> requestD = new RequestE();
    MyBaseClass resultD = requestD.execute().getResult();
    MyConcreteClass resultD2 = (MyConcreteClass) resultD;

为什么我不能跳过变量的泛型类型声明?

AbstractRequest request = new RequestA(); 
Integer resultC = request.execute().getResult(); // compile error

如果您没有显式声明泛型类型,Java将把它作为Object来处理。因此getResult()将返回一个Object。因为Java是强类型语言,所以不允许在不进行转换的情况下将Object放入Integer变量。没有解决方法。


响应类型与请求绑定,以避免在使用时声明类型声明。如果一个请求类型可以返回不同的响应类型,则可能没有足够好的封装,您应该将其拆分为两种不同的请求类型或重构响应类型。

我假设您已经知道如何获取HTTP响应,因此我跳过了那部分。

/**
 * Response is a generic wrapper, which could contain any value.
 */
class Response<RETURN_TYPE> {
    private final RETURN_TYPE result;

    public Response(RETURN_TYPE result) {
        this.result = result;
    }

    public RETURN_TYPE getResult() {
        return result;
    }

    // Could contain additional meta data, like status code or warnings.
}

/**
 * AbstractRequest does the main work. Subclasses of AbstractRequest just
 * provide request parameters.
 */
abstract class AbstractRequest<RETURN_TYPE> {
    private final Class<RETURN_TYPE> returnType;

    /**
     * Return type has to be set explicitly, because the JSON parser needs
     * to know what class it should instantiate and type erasure prevents
     * accessing the generic type at runtime.
     */
    protected AbstractRequest(Class<RETURN_TYPE> returnType) {
        this.returnType = returnType;
    }

    /**
     * Request-dependent parameters must be set in sub classes.
     */
    protected abstract String getRequestUrl();

    public Response<RETURN_TYPE> execute() throws IOException {
        // I'll skip the details. You already know how to get here.
        InputStream response = ... ;

        // In real code you should reuse JsonFactory .
        JsonParser parser = new JsonFactory().createJsonParser(response);

        // Wrap it into a Response.
        return new Response<RETURN_TYPE>(parser.readValueAs(this.returnType));
    }
}

// Examples:

class RequestA extends AbstractRequest<Integer> {
    public RequestA() {
        super(Integer.class);
    }

    protected String getRequestUrl() {
        return "http://example.org/a";
    }
}

static class RequestB extends AbstractRequest<String> {
    public RequestB() {
        super(String.class);
    }

    ...
}

P.S。如果你不喜欢子类AbstractRequest,你可以使它非抽象并直接实例化它。在这种情况下,您可以将Diamond运算符与Java 7及更高版本一起使用:

    AbstractRequest<String> request = new AbstractRequest<>();

答案 3 :(得分:1)

使用泛型,你可以这样做:

public class SomeClass { 
    private Object body;

    @SuppressWarnings("unchecked")
    public <T> T getBody(Class<T> type) {
        if(type.isAssignableFrom(body.getClass())) {
            return (T) body;
        }

        return null;
    }

    public void setBody(Object body) {
        this.body = body;
    }
}

它仍然涉及转换为T,但至少你是在一个方法的内容中执行它,而不是经常检查类类型和转换返回值。

答案 4 :(得分:0)

也许更多的OO方法是:

public interface Protocol{
 public enum Type{ SAMPLE_TYPE } //define types of protocol for ex: message, file transfer, etc...
 Type getType();
 Object[] getParams();
 Protocol execute();
}

public MyProtocol implements Protocol{
 private Type type;
 private Object[] params;

 public MyProtocol(Type t, Object... params){
  this.type = t;
  this.params = params;
 }

 public Protocol execute(){
  switch(this.type){
   case SAMPLE_TYPE:{
    //Your implementation here
    break;
  }
 }

 public Type getType(){ return Type; }
 public Object[] getParams(){ return params; }
}

这样你可以使用以下内容:

int requestTypeA() {
 int someNeededValueForExecution = 1337;
 String someNeededStringForExecution = "This is an example";
 Protocol request = new MyProtocol(Protocol.Type.SAMPLE_TYPE, someNeededValueForExecution, someNeededStringForExecution);
 Protocol response = request.execute();
 // You can have multiple parameters too, parse them or return them to caller
 return (int)response.getParams()[0];
}

答案 5 :(得分:0)

每个Type都有一个特定的内容类型的响应。这应该在代码中表示,并且可以使用泛型完成。

interface Type<T> {}

可以将不同类型定义为常量。

interface Type<T> {
    Type<Integer> A = new Type<Integer>(){};
    Type<String>  B = new Type<String>(){};
}

请求的实例化变化很小。

Request request = new Request(Type.A);

可以修改响应以使用Type访问其内容。

interface Response {
    <T> T getContent(Type<T> type);
}

这种方式不需要施法。

int requestTypeA() {
    Request request = new Request(Type.A);
    Response response = request.execute();
    return response.getContent(Type.A);
}

String requestTypeB() {
    Request request = new Request(Type.B);
    Response response = request.execute();
    return response.getContent(Type.B);
}

大多数IDE或编译器都会报告Type与响应的内容类型之间的任何不匹配。


Response可以作为通用类实现。

class GenericResponse<C> implements Response {
    private final Type<C> type;
    private final C content;

    public GenericResponse(Type<C> type, C content) {
        this.type = type;
        this.content = content;
    }

    @Override
    public <T> T getContent(Type<T> type) {
        if (this.type == type)
            return (T) content;
        else
            throw new IllegalArgumentException();
    }
}

Request可以使用多态实现。

interface Request {
    Response execute();
}

class RequestTypeA implements Request {
    @Override
    public Response execute() {
        // do stuff
        return new GenericResponse<Integer>(Type.A, 123);
    }
}

class RequestTypeB implements Request {
    @Override
    public Response execute() {
        // do stuff
        return new GenericResponse<String>(Type.B, "done");
    }
}

请求的实例化可以移至Type

interface Type<T> {
    Type<Integer> A = new Type<Integer>(){
        @Override
        public Request createRequest() {
            return new RequestTypeA();
        }
    };

    Type<String>  B = new Type<String>(){
        @Override
        public Request createRequest() {
            return new RequestTypeB();
        }
    };

    Request createRequest();
}

以下是生成的方法调用。

int requestTypeA() {
    Request request = Type.A.createRequest();
    Response response = request.execute();
    return response.getContent(Type.A);
}

String requestTypeB() {
    Request request = Type.B.createRequest();
    Response response = request.execute();
    return response.getContent(Type.B);
}

答案 6 :(得分:-1)

我使用enum来存储所有可能的返回类型。

public enum Type {
INT, STRING
}

定义Request和Response类的子类。

Request类的每个子类都会覆盖其execute方法并返回其对应的Response子类实例。

public class RequestINT extends Request {
public RequestINT(){
    super(Type.INT);
}
@Override
public Response execute() {
    return new ResponseINT();
}
}


public class ResponseINT extends Response {
@Override
public Type getResponse() {
    return Type.INT;
}
}

最后在你的调用方法中使用它

public class TestExec {

public static void main(String[] args) {

    Request request1 = new RequestINT();
    Response response1 = request1.execute();
    System.out.println(response1.getResponse());

    Request request2 = new RequestSTRING();
    Response response2 = request2.execute();
    System.out.println(response2.getResponse());

}

}

答案 7 :(得分:-1)

这样怎么样?

package com.example.stackoverflow.oop;

public class Executor {

    public static void main(String[] args) throws Exception  {
        String req = "helloworld";
        String res = execute(req, String.class);
        System.out.println( "Res:" + res );
    }

    public static <T,R> R execute(T req, Class<R> res) throws Exception {
        System.out.println(req.toString());
        Object object = res.newInstance();
        return res.cast(object);
    }

}
-------------------------------------------------------------------------
helloworld
Res:

我使用字符串作为响应,因为整数需要一个参数。

答案 8 :(得分:-1)

保持简单:

interface Request<RETURNVALUE>{Response<RETURNVALUE> execute();}

interface Response<RETURNVALUE>{RETURNVALUE getValue();}

//IMPL
class Client{

String requestTypeA(){
    Request<String> q = new RequestA();
    return q.execute().getValue();
}
}

class RequestA implements Request<String>{

@Override
public Response<String> execute() {
    return new ResponseA();
}

}

class ResponseA implements Response<String>{

@Override
public String getValue() {
    return null;
}
}