MVC,MVP,MVVM的理解和区别

MVC

MVC ,早期的开发架构,在安卓里,用res代表V,activity代表Controller层,Model层完成数据请求,更新操作,activity完成view的绑定,以及业务逻辑的编写,更新view,这种模式是单向的,虽然代码存在分层,但是之间的耦合性还是很高,并且C层做的事情很多,久而久之会变得臃肿。

MVC,MVP,MVVM的理解和区别_第1张图片

代码实现:

ViewModel

   static class  StudentModel{

        private String name;
        private int age;

        public StudentModel(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

View层

    static class  StudentView{

       public  void PrintStudentData(String name ,int age){
           System.out.println("Student name:"+name+"   age:"+age);
       }

    }

Controller

static class StudentController{
        StudentModel model;
        StudentView view;

        public StudentController(StudentModel model, StudentView view) {
            this.model = model;
            this.view = view;
        }

        public void setStudentName(String name){
            model.setName(name);
        }

        public String getStudentName(){
            return model.getName();
        }

        public void setStudentAge(int age){
            model.setAge(age);
        }

        public  Integer getStudentAge(){
            return model.getAge();
        }

        public  void updateView(StudentView view){
            view.PrintStudentData(model.getName(),model.getAge());
        }


    }

完整代码

package com.example.lib;

public class MvcTest {

    public static void main(String[] args) {
        StudentModel student = getStudentDataBase();
        StudentView view = new StudentView();

        StudentController controller = new StudentController(student,view);
        controller.updateView(view);

        controller.setStudentName("lisi");
        controller.updateView(view);

    }

    public static StudentModel getStudentDataBase(){
        return new StudentModel("zhangsan",18);
    }

   static class  StudentModel{

        private String name;
        private int age;

        public StudentModel(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }


    static class  StudentView{

       public  void PrintStudentData(String name ,int age){
           System.out.println("Student name:"+name+"   age:"+age);
       }

    }

   static class StudentController{
        StudentModel model;
        StudentView view;

        public StudentController(StudentModel model, StudentView view) {
            this.model = model;
            this.view = view;
        }

        public void setStudentName(String name){
            model.setName(name);
        }

        public String getStudentName(){
            return model.getName();
        }

        public void setStudentAge(int age){
            model.setAge(age);
        }

        public  Integer getStudentAge(){
            return model.getAge();
        }

        public  void updateView(StudentView view){
            view.PrintStudentData(model.getName(),model.getAge());
        }


    }


}

MVC,MVP,MVVM的理解和区别_第2张图片

MVP

于是,出现了MVP模式,这个架构模式在安卓移动端开发中比较常见,将Activity和res统一作为V层,Model层还是完成数据的请求和更新,新建了一个类Presenter,用来作为P层,这样的好处是,方便管理每一个Activity的业务逻辑,拿到数据后,通过接口回调的方式,更新View层,实现了双向通信。缺点是需要实现Presenter类,并且需要开发接口,对简单的业务逻辑比较繁琐,且P层的事情还是比较臃肿

MVC,MVP,MVVM的理解和区别_第3张图片

代码实现(以登录事务为例):

ViewModel层

相比与MVC的ViewModel,MVP的不同在于ViewModle多实现了一个IUser接口,从而实现双向通信

    interface IUser{
        void login(String name,String password,OnLoginListener listener);
    }


    interface OnLoginListener{
        void loginFail();
        void loginSuccess(User user);
    }

    static class UserModel implements IUser{
        @Override
        public void login(String name, String password, OnLoginListener listener) {
            //模拟一下登录
            if(name.equals("admin")&&password.equals("123456")){
                User user = new User();
                user.setUsername(name);
                user.setPassword(password);
                listener.loginSuccess(user);
            }else{
                listener.loginFail();
            }
        }
    }

View层:

在Android开发中,MVP架构模式里,View层指的是Activity和res。同理,也实现了一个IUserLogin接口,从而和P层实现双向通信

 static class UserView implements IUserLogin{
        @Override
        public void showLoading() {
            System.out.println("正在登录...");
        }

        @Override
        public void hideLoading() {
            System.out.println("登录完成,获取结果");
        }

        @Override
        public void loginFail() {
            System.out.println("登录失败");
        }

        @Override
        public void loginSuccess(User user) {
            System.out.println("username:"+ user.getUsername()+" 登陆成功");
        }
    }

    interface IUserLogin{
        void showLoading();
        void hideLoading();
        void loginFail();
        void loginSuccess(User user);
    }

Presenter层

 static class UserPresenter {

        private final UserModel userModel;
        private final UserView userView;
        private final User user;

        public UserPresenter(UserView userView) {
            this.userModel = new UserModel();
            this.userView = userView;
            user = new User("admin","123456");
        }

        void login(){
            userView.showLoading();
            userModel.login(user.getUsername(), user.getPassword(), new OnLoginListener() {
                @Override
                public void loginFail() {
                    userView.hideLoading();
                    userView.loginFail();
                }

                @Override
                public void loginSuccess(User user) {
                    userView.hideLoading();
                    userView.loginSuccess(user);
                }
            });
        }
    }

完整代码如下:

package com.example.lib;

public class MvpTest {

    public static void main(String[] args) {

        UserView loginView  = new UserView();
        UserPresenter presenter = new UserPresenter(loginView);

        presenter.login();
    }

   static class User{
        String username;
        String password;

       public User() {

       }

       public User(String username, String password) {
           this.username = username;
           this.password = password;
       }

       public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }


    interface IUser{
        void login(String name,String password,OnLoginListener listener);
    }


    interface OnLoginListener{
        void loginFail();
        void loginSuccess(User user);
    }

    static class UserModel implements IUser{
        @Override
        public void login(String name, String password, OnLoginListener listener) {
            //模拟一下登录
            if(name.equals("admin")&&password.equals("123456")){
                User user = new User();
                user.setUsername(name);
                user.setPassword(password);
                listener.loginSuccess(user);
            }else{
                listener.loginFail();
            }
        }
    }

    static class UserView implements IUserLogin{
        @Override
        public void showLoading() {
            System.out.println("正在登录...");
        }

        @Override
        public void hideLoading() {
            System.out.println("登录完成,获取结果");
        }

        @Override
        public void loginFail() {
            System.out.println("登录失败");
        }

        @Override
        public void loginSuccess(User user) {
            System.out.println("username:"+ user.getUsername()+" 登陆成功");
        }
    }

    interface IUserLogin{
        void showLoading();
        void hideLoading();
        void loginFail();
        void loginSuccess(User user);
    }


    static class UserPresenter {

        private final UserModel userModel;
        private final UserView userView;
        private final User user;

        public UserPresenter(UserView userView) {
            this.userModel = new UserModel();
            this.userView = userView;
            user = new User("admin","123456");
        }

        void login(){
            userView.showLoading();
            userModel.login(user.getUsername(), user.getPassword(), new OnLoginListener() {
                @Override
                public void loginFail() {
                    userView.hideLoading();
                    userView.loginFail();
                }

                @Override
                public void loginSuccess(User user) {
                    userView.hideLoading();
                    userView.loginSuccess(user);
                }
            });
        }
    }



}

MVC,MVP,MVVM的理解和区别_第4张图片

MVVM

再后面,就是MVVM,同理,Activity和res还是作为V层,Model层请求数据和更新,出现了一个ViewModel的层,这个类的职责在于,对数据进行绑定,以数据驱动View层,同样,当视图层改变后,Model层也能更新,从而实现了M和V之间的双向绑定。最常用的是谷歌2015年推出的jetpack的组件DataBinding,无须进行view绑定,避免了空指针等很多繁琐的业务逻辑。

代码实现:略,用JetPack提供的databinding即可

总结
MVC,MVP,MVVM的出现,都是为了视图分层,使代码结构,逻辑变得清晰,同时,实现类的单一职责,降低耦合度。但是同时选用架构设计模式的时候,也要根据业务类型来考虑,避免出现大多繁琐的设计,总之,一切为了开发效率。

你可能感兴趣的:(Android基础,mvc)