- /**
- * 抽象的策略,定义逃跑路线
- */
- public interface IRunStrategy {
- // 逃跑线路
- void escapeRoute();
- }
- /**
- * 具体的策略一走大路
- */
- public
- class
- Highroad
- implements
- IRunStrategy
- {
- @Override
- public
- void
- escapeRoute
- ()
- {
- System.out.println("走大路");
- }
- }
- /**
- * 具体的策略二走华容道
- */
- public
- class
- HuaRongRoad
- implements
- IRunStrategy
- {
- @Override
- public
- void
- escapeRoute
- ()
- {
- System.out.println("走华容道");
- }
- }
- /**
- * 上下文 持有 IRunStrategy 的引用
- */
- public class ContextRunStrategy {
- private IRunStrategy iRunStrategy ;
- public ContextRunStrategy(IRunStrategy iRunStrategy){
- this.iRunStrategy = iRunStrategy ;
- }
- /**
- * 选择道路
- */
- public
- void
- choiceRoad
- ()
- {
- iRunStrategy.escapeRoute();
- }
- }
- /**
- * 曹操选择路线
- */
- public class CaoCao {
- public
- static
- void
- main
- (String args[])
- {
- /**
- * 曹操疑心重,选择了华容道,对曹操来说至于杂样走华容道,不关心,死多少人也不关心,只关心我要走这条道就好
- */
- IRunStrategy huaRongRoad = new HuaRongRoad() ;
- ContextRunStrategy contextRunStrategy = new ContextRunStrategy(huaRongRoad) ;
- contextRunStrategy.choiceRoad();
- }
- }
- compile 'com.squareup.retrofit2:retrofit:2.3.0'
- compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
- compile 'io.reactivex.rxjava2:rxjava:2.1.5'
- compile 'com.loopj.android:android-async-http:1.4.9'
- /**
- * @Description MVP 中的 Presenter 基
- * @Creator TigerChain(创建者)
- */
- public interface Presenter {
- }
- /**
- * @Description MVP 中的 Model 基类
- * @Creator TigerChain(创建者)
- */
- public interface Model {
- }
- /**
- * @Description MVP 中的 View 基类
- * @Creator TigerChain(创建者)
- */
- public interface IView {
- }
- package designpattern.tigerchain.com.mvphttpstrategy.mvp;
- import designpattern.tigerchain.com.mvphttpstrategy.mvp.domain.User;
- import io.reactivex.Observable;
- /**
- * @Description MVP 的关联类「也可以单独创建 MVP 就是有点乱」
- * @Creator TigerChain(创建者)
- */
- public interface ILoginContact {
- interface
- LoginView
- extends
- IView
- {
- //显示进度条
- void showProgress() ;
- //隐藏进度条
- void hideProgress() ;
- //登录成功
- void loadSuccess(String str) ;
- //登录失败
- void loadFailed(String str) ;
- //取得用户名
- String getUserName() ;
- //取得用户密码
- String getUserPass() ;
- //清除输入框
- void clearEditText() ;
- //用户名和密码不能为空
- void editnotNull() ;
- }
- interface
- LoginPresenter
- extends
- Presenter
- {类
- /**
- * 登录功能
- */
- void login() ;
- /**
- * 清除输入框架内容
- */
- void clear() ;
- }
- interface
- ILoginModel
- extends
- Model
- {
- /***
- * 登录的方法,其实这里就是一个抽象策略,至于你使用 retrofit 还是 asynchttpClient 还是 Volley 那是自己的事情
- * @param uName
- * @param uPass
- * @return
- */
- Observable<User> login(String uName, String uPass) ;
- }
- }
- /**
- * @Description 具体策略使用 AsyncHttpClient 来调用登录 API
- * @Creator TigerChain(创建者)
- */
- public
- class
- AsynchHppClientImplLogimModel
- implements
- ILoginContact
- .
- ILoginModel
- {
- @Override
- public
- Observable<User>
- login
- (final String uName, final String uPass)
- {
- return Observable.create(new ObservableOnSubscribe<User>() {
- @Override
- public
- void
- subscribe
- (final ObservableEmitter<User> e)
- throws
- Exception
- {
- AsyncHttpClient client = new AsyncHttpClient() ;
- // 这里就是一个请求 没有真正的对接服务器,只是一个演示
- client.get("http://www.baidu.com", new AsyncHttpResponseHandler() {
- @Override
- public
- void
- onSuccess
- (int statusCode, Header[] headers, byte[] responseBody)
- {
- if(uName.equalsIgnoreCase("TigerChain") && uPass.equals("123")){
- User user = new User() ;
- user.setuName(uName);
- user.setUpass(uPass);
- e.onNext(user);
- e.onComplete();
- }else{
- e.onNext(null);
- e.onComplete();
- }
- }
- @Override
- public
- void
- onFailure
- (int statusCode, Header[] headers, byte[] responseBody, Throwable error)
- {
- e.onError(error);
- }
- }) ;
- }
- });
- }
- }
- /**
- * @Description 具体策略使用 Volley 实现登录功能
- * @Creator TigerChain(创建者)
- */
- public
- class
- VolleyImplLoginModel
- implements
- ILoginContact
- .
- ILoginModel
- {
- @Override
- public
- Observable<User>
- login
- (final String uName, final String uPass)
- {
- return Observable.create(new ObservableOnSubscribe<User>() {
- @Override
- public
- void
- subscribe
- (final ObservableEmitter<User> e)
- throws
- Exception
- {
- /***
- * 这里调用和 Volley 相关的 API 实现登录即可
- */
- }
- });
- }
- }
- /**
- * @Description 具体策略 使用 RetorFit 实现登录功能性
- * @Creator TigerChain(创建者)
- */
- public
- class
- RetorFitImplLoginModel
- implements
- ILoginContact
- .
- ILoginModel
- {
- @Override
- public
- Observable<User>
- login
- (final String uName, final String uPass)
- {
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl("http://")
- .build();
- ILoginRetorfitApi loginService = retrofit.create(ILoginRetorfitApi.class) ;
- return loginService.login(uName,uPass) ;
- }
- }
- #User.java
- /**
- * @Description 普通人的 Java
- * @Creator TigerChain(创建者)
- */
- public class User {
- private String uName;
- private String Upass;
- public String getuName() {
- return uName;
- }
- public void setuName(String uName) {
- this.uName = uName;
- }
- public String getUpass() {
- return Upass;
- }
- public void setUpass(String upass) {
- Upass = pass;
- }
- }
- # ILoginRetorfitApi.java
- /**
- * @Description Retrofit API
- * @Creator TigerChain(创建者)
- */
- public interface ILoginRetorfitApi {
- @GET("/login")
- Observable<User> login( @Field("userName") String userName,
- @Field("passWord")String passWord) ;
- }
- /**
- * @Description MVP 中的P ,就相当于策略中Context
- * @Creator junjun(创建者)
- */
- public
- class
- LoginPresenterImpl
- implements
- ILoginContact
- .
- LoginPresenter
- {
- private ILoginContact.ILoginModel iLoginModel ;
- private ILoginContact.LoginView loginView ;
- public LoginPresenterImpl(ILoginContact.LoginView loginView,ILoginContact.ILoginModel iLoginModel){
- this.iLoginModel = iLoginModel ;
- this.loginView = loginView ;
- }
- @Override
- public
- void
- login
- ()
- {
- String uName = loginView.getUserName() ;
- String uPass = loginView.getUserPass() ;
- if(TextUtils.isEmpty(uName) || TextUtils.isEmpty(uPass)){
- loginView.editnotNull();
- return ;
- }
- loginView.showProgress();
- iLoginModel.login(uName,uPass)
- // subscribeOn(Schedulers.io()) 由于 AsyncHttpClient 本身就是在子线程去请求的,所以这里为了演示把这个去掉
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<User>() {
- @Override
- public
- void
- onSubscribe
- (Disposable d)
- {
- }
- @Override
- public
- void
- onNext
- (User user)
- {
- loginView.loadSuccess("登录成功");
- }
- @Override
- public
- void
- onError
- (Throwable e)
- {
- loginView.loadFailed("用户名或密码错误,登录失败");
- loginView.hideProgress();
- }
- @Override
- public
- void
- onComplete
- ()
- {
- loginView.hideProgress();
- }
- }) ;
- }
- @Override
- public
- void
- clear
- ()
- {
- loginView.clearEditText();
- }
- }
来源: https://juejin.im/post/5a02fda7518825603a380dec