我正在尝试找到最佳的OO方法来执行此操作,感谢您的帮助。

我认为最简单的方法是向您展示我是如何做到的,然后尝试解释我想要的东西(我简化了):

abstract public class MyServiceApi {
    private static MyServiceApi instance = null;

    public static <T extends MyServiceApi> T getInstance(Class<T> cls) {
        if (instance == null) {
            try {
                instance = cls.newInstance();
            }
            catch (InstantiationException e) {}
            catch (IllegalAccessException e) {}
        }

        return (T) instance;
    }

    private private HashMap<String, String> headers;

    protected MyServiceApi() {}

    public HashMap<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(HashMap<String, String> headers) {
        this.headers = headers;
    }

    protected <T extends IMyServiceApiResponse> T send(String url, IMyServiceApiRequest request, Class<T> to) {
        // Do some stuffs

        // IMPORTANT : Also set headers to the request
    }

    protected String getBaseUrl() {
        return "http://api.mywebsite.com/";
    }
}

public class UsersApi extends MyServiceApi {
    public static UsersApi getInstance() {
        return getInstance(UsersApi.class);
    }

    protected UsersApi() {
        super();
    }

    @Override
    protected String getBaseUrl() {
        return super().getBaseUrl() + "Users/";
    }

    // mutliple function that calls a specific URL in the API, and return specifics object based on the call, for example :
    public MyServiceApiUsersResponse getUsers(MyServiceApiUsersRequest request) {
        return send(getBaseUrl() + "get", request, MyServiceApiUsersResponse.class);
    }
}

public class ItemsApi extends MyServiceApi {
    public static ItemsApi getInstance() {
        return getInstance(ItemsApi.class);
    }

    protected ItemsApi() {
        super();
    }

    @Override
    protected String getBaseUrl() {
        return super().getBaseUrl() + "Items/";
    }

    // mutliple function that calls a speicfic URL in the API, and return specifics object based on the call, for example :
    public MyServiceApiItemsResponse getUsers(MyServiceApiItemsRequest request) {
        return send(getBaseUrl() + "get", request, MyServiceApiItemsResponse.class);
    }

}

现在您有了主意,我被困住了。

首先,我不知道我所做的是否正确(以Java OO方式)。我认为这还不错,但是我缺乏确定的经验。

第二,一旦我的项目运行,MyServiceApi将保留相同的头,我不会调用其他API或使用其他凭据。这就是为什么我想到Singleton的原因:我在应用程序启动时设置了标头,然后只需要执行请求即可。
但我相信让UsersApiItemsApi扩展MyServiceApi是最好的方法。他们使用MyServiceApi,没有扩展其功能。
另外,我也很担心SingleTon是反模式的,不利于测试等。

所以现在我很放松,我不知道该怎么办。你会怎么做?

一个可能的想法是删除MyServiceApi的摘要并在其上设置一个Singleton,让UsersApiItemsApi使用MyServiceApi,但不通过扩展它,但是我将如何管理getBaseUrl呢?

非常感谢您的帮助,非常感谢!

最佳答案

我就是这样写的

enum MyServiceApi {
    UsersApi {
        public MyServiceApiUsersResponse getUsers(MyServiceApiUsersRequest request) {
            return send(getBaseUrl() + "Users/get", request, MyServiceApiUsersResponse.class);
        }

    },  ItemsApi {
        // mutliple function that calls a speicfic URL in the API, and return specifics object based on the call, for example :
        public MyServiceApiItemsResponse getUsers(MyServiceApiItemsRequest request) {
            return send(getBaseUrl() + "Items/get", request, MyServiceApiItemsResponse.class);
        }
    };

    private final Map<String, String> headers = new LinkedHashMap<String, String>();

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(HashMap<String, String> headers) {
        this.headers.clear();
        this.headers.putAll(headers);
    }

    public abstract <T extends IMyServiceApiResponse> T send(String url, IMyServiceApiRequest request, Class<T> to);

    protected String getBaseUrl() {
        return "http://api.mywebsite.com/";
    }
}

10-04 10:59