公司项目中部分SDK的架构方式(beta 2.0)

mvp模式实战

Posted by Mio4kon on 2014-09-12

引言:为了使公司的支付等功能,多个APP可以公用,将其业务功能封装到SDK中.减少开发成本.
该SDK的编写基于MVP模式.MVP相对于MVC模式的好处在于,方便测试,View层代码避免臃肿等.

model层使用Volley进行访问网络.

public class UserModel extends BaseRequestModel implements IUserModel {
    private String url;
    private Map<String, String> requestMap;

    @Override
    public void login(String username, String password) {
        url = NetConstantValue.HOST_URL + NetConstantValue.LOGIN_Url;
        requestMap = new HashMap<String, String> ();
        postMethod (url,requestMap, _BasicUserInfo.class,new Response.Listener<_BasicUserInfo> () {
            @Override
            public void onResponse(_BasicUserInfo response) {
                if(response.flag == 1){ //login success
                    _BasicUserInfo.BasicUserInfo basicUserInfo = response.data;
                    NotificationCenter.defaultCenter ().postNotification (ConsNotification.Login,basicUserInfo);
                    return;
                }
                NotificationCenter.defaultCenter ().postNotification (ConsNotification.Login,null);
            }
        },new Response.ErrorListener () {
            @Override
            public void onErrorResponse(VolleyError error) {
                NotificationCenter.defaultCenter ().postNotification (ConsNotification.Login,null);
            }
        });
    }
 }

其中NotificationCenter利用的是观察者模式(发布-订阅模式),通过添加观察者接收网络请求成功/失败的消息.

NotificationCenter的思想在于创建一个map集合,放入通知类型和观察者

public class NotificationCenter {
    private HashMap<String, DummyObservable> observables;
    private static NotificationCenter instance = null;
    public static NotificationCenter defaultCenter() {
        if (instance == null) {
            instance = new NotificationCenter();
        }
        return instance;
    }

    public NotificationCenter() {
        observables = new HashMap<String, DummyObservable>();
    }

    public void addObserver(String notification, Observer observer) {
        DummyObservable observable = observables.get(notification);
        if (observable == null) {
            observable = new DummyObservable(notification);
            observables.put(notification, observable);
        }
        observable.addObserver(observer);
    }

    public void removeObserver(String notification, Observer observer) {
        DummyObservable observable = observables.get(notification);
        if (observable != null) {
            observable.removeObserver(observer);
        }
    }

    public void postNotification(String notification, Object object) {
        DummyObservable observable = observables.get(notification);
        if (observable != null) {
            if (object == null) {
                observable.notifyObservers();
            } else {
                observable.notifyObservers(object);
            }
        }
    }

    class DummyObservable {
        private String _name;
        private List<Observer> _list = new ArrayList<Observer>();

        DummyObservable(String name) {
            _name = name;
        }
        public void addObserver(Observer observer) {
            _list.add(observer);
        }
        public void removeObserver(Observer observer) {
            _list.remove(observer);
        }
        public void notifyObservers(Object object) {
            List<Observer> iterList = new ArrayList<Observer>(_list);
            for(Observer item : iterList) {
                if (item == null) {
                    _list.remove(item);
                    Log.i("NotificationCenter", _name+":WeakReference deleted by GC");
                } else {
                    item.update(null, object);
                }
            }
        }
        public void notifyObservers() {
            notifyObservers(null);
        }
    }
}

presenter层是对Model层的反馈并将反馈信息传到View层.View层通过接口实现获取信息以及对信息的展示.

public class LoginPresenter {
    private ILoginActivity mLoginActivity;
    private IUserModel mUserModel;
    public LoginPresenter(ILoginActivity loginActivity) {
        mLoginActivity = loginActivity;
        mUserModel = new UserModel ();
    }

      /**登陆方法**/
    public observer login(String username,String password){
        Observer observer;
        mUserModel.login (username,md5Password);
        NotificationCenter.defaultCenter ().addObserver (ConsNotification.Login,observer =new Observer () {
            @Override
           public void update(Observable observable, Object data) {
                _BasicUserInfo.BasicUserInfo userInfo = (_BasicUserInfo.BasicUserInfo) data;
                mLoginActivity.loginCallback (userInfo);
                } 
            }
        });
    }

View层的接口,很简单,一般对应界面的功能.

public interface ILoginActivity {

    public void loginCallback(_BasicUserInfo.BasicUserInfo userInfo);

}

View层仅仅处理显示逻辑

public class LoginActivity extends BaseActivity implements ILoginActivity {

@Override
protected void onCreate() {
    setContentView (R.layout.activity_login);
    ButterKnife.inject (this);
    mLoginPresenter = new LoginPresenter (this);
}

@Override
public void loginCallback(_BasicUserInfo.BasicUserInfo userInfo) {
           //TODO
    }

@Override
public void onClick(View v) {
    int id = v.getId ();
    switch (id) {
        case R.id.br_login: // 登录
            mLoginPresenter.login (username,password);
            break;
    }
}

 }

最后记得当Observer使用结束后回收Observer.