美文网首页
代理模式-android快速切换三方框架

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

作者: 狂奔的_土豆 | 来源:发表于2020-05-15 17:35 被阅读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);
        }
    }
    

    相关文章

      网友评论

          本文标题:代理模式-android快速切换三方框架

          本文链接:https://www.haomeiwen.com/subject/ygxhohtx.html