设计模式之工厂模式使用
来源:互联网 发布:15年nba总决赛个人数据 编辑:程序博客网 时间:2024/06/05 16:23
实际应用:
接口解耦(快速更换框架,不用改动太多代码)
示例:更换网络请求框架
来自:https://v1sk.github.io/archives/
思路:
1.把http请求框架的共性方法抽取到接口中,我们把这个接口称为“请求接口”;
2.创建一个用于返回请求结果的接口,我们把这个接口称为“回调接口”;
3.分别用Volley和OkHttp实现“请求接口”;
4.创建一个类来返回上述接口的对象,我们把这个类叫做“工厂”类;
5.在Activity/Fragment/Presenter中,使用“工厂”返回的这个接口对象调用get/post/put/delete方法,并在“回调接口”中得到请求结果。
代码
1.把http请求框架的共性方法抽取到接口中
/* * 此接口提供的就是http请求通用的方法,该接口可以用Volley来实现,也能用OkHttp等其它方式来实现 * 接口说明: * get方法参数包含一个url,以及返回数据的接口 * post/put/delete方法还需要提供一个请求体参数 */public interface IRequestManager { void get(String url, IRequestCallback requestCallback); void post(String url, String requestBodyJson, IRequestCallback requestCallback); void put(String url, String requestBodyJson, IRequestCallback requestCallback); void delete(String url, String requestBodyJson, IRequestCallback requestCallback);}
上面的IRequestManager接口中用到的请求结果回调接口——IRequestCallback
/** * Created by chenjianwei on 2016/12/11. * 请求返回成功/失败,成功时,把服务器返回的结果回调出去,失败时回调异常信息 * onSuccess中的参数类型,当然也可以为JSONObject,这里只是举个栗子,可按照实际需求变通 */public interface IRequestCallback { void onSuccess(String response); void onFailure(Throwable throwable);}
VolleyRequestManager
public class VolleyRequestManager implements IRequestManager { ... @Override public void get(String url, final IRequestCallback requestCallback) { StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String s) { requestCallback.onSuccess(s); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { requestCallback.onFailure(volleyError); } }); HttpRequestExampleApp.mQueue.add(request); } @Override public void post(String url, String requestBodyJson, final IRequestCallback requestCallback) { requestWithBody(url, requestBodyJson, requestCallback, Request.Method.POST); } @Override public void put(String url, String requestBodyJson, final IRequestCallback requestCallback) { requestWithBody(url, requestBodyJson, requestCallback, Request.Method.PUT); } @Override public void delete(String url, String requestBodyJson, final IRequestCallback requestCallback) { requestWithBody(url, requestBodyJson, requestCallback, Request.Method.DELETE); } ...}
工厂类RequestFactory
/* * 该类的作用是用于返回一个IRequestManager对象,这个IRequestManager的实现类 * 可以是使用Volley实现的http请求对象,也可以是OkHttp实现的http请求对象 * Activity/Fragment/Presenter中,只要调用getRequestManager()方法就能得到 * http请求的操作接口,而不用关心具体是使用什么实现的。 */public class RequestFactory { public static IRequestManager getRequestManager(){ return VolleyRequestManager.getInstance(); //return OkHttpRequestManager.getInstance(); }}
使用RequestFactory的方式在Activity/Fragment/Presenter中创建对象,代码只依赖了IRequestManager这个接口,这就是使用接口进行解耦的关键点,无论在什么地方使用了这个接口,当要更换实现的时候,只需要修改RequestFactory中return的实现类就可以了。
测试代码
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //请求测试url String url = "https://api.douban.com/v2/movie/top250"; //这里发起请求依赖的是IRequestManager接口,而没有具体实现类代码出现在这里 IRequestManager requestManager = RequestFactory.getRequestManager(); //调用接口的get方法 requestManager.get(url, new IRequestCallback() { @Override public void onSuccess(String response) { Log.d(TAG, "onSuccess: " + response); //// TODO: 2016/12/11 your code } @Override public void onFailure(Throwable throwable) { throwable.printStackTrace(); //// TODO: 2016/12/11 your code } }); }}
1 0
- 设计模式之工厂模式使用
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之---工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之-工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之 ------工厂模式
- chrom google 浏览器 调试时js无法刷新的解决方法
- codevs 3185 队列练习1
- Action
- 将博客搬至CSDN
- hibernate get() load() 区别
- 设计模式之工厂模式使用
- Docker 中如何连接多个 Container 协同工作
- 深入Java对象的比较
- 矩形嵌套
- ZOJ 3953 Intervals
- NYOJ 1239 引水工程
- Codeforces 801A Vicious Keyboard
- 关于项目SSH 框架的一点想法
- IDEA使用快捷键