代理模式-android快速切换三方框架

2020-05-15  本文已影响0人  狂奔的_土豆
定义

为其他对象提供一种代理以控制对这个对象的访问

package com.code.app.http;

import java.util.Map;

public interface IHttpProcessor {

    void post(String url, Map<String, Object> params, ICallback callback);

    void get(String url, Map<String, Object> params, ICallback callback);
}
package com.code.app.http;

public interface ICallback {

    void onSuccess(String result);

    void onFailure(HttpError e);

}
package com.code.app.http;

public class HttpError {

    private int code;

    private int message;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code= code;
    }

    public int getMessage() {
        return message;
    }

    public void setMessage(int message) {
        this.message = message;
    }
}
package com.code.app.http;

import java.util.Map;

public class HttpHelper implements IHttpProcessor {

    private static HttpHelper instance;

    private static IHttpProcessor mIHttpProcessor;

    private HttpHelper() {
    }

    public static HttpHelper obtain() {
        if (instance == null) {
            synchronized (HttpHelper.class) {
                if (instance == null) {
                    instance = new HttpHelper();
                }
            }
        }
        return instance;
    }

    public void init(IHttpProcessor iHttpProcessor) {
        mIHttpProcessor = iHttpProcessor;
    }


    @Override
    public void post(String url, Map<String, Object> params, ICallback callback) {
        mIHttpProcessor.post(url, params, callback);
    }

    @Override
    public void get(String url, Map<String, Object> params, ICallback callback) {
        mIHttpProcessor.get(url, params, callback);
    }
}
上一篇下一篇

猜你喜欢

热点阅读