Android 之设计模式Mvc、Mvp和Mvvm

Android 之设计模式Mvc、Mvp和Mvvm_第1张图片

这篇文章记录一下Android中框架的Mvc、Mvp、Mvvm模式。在面试中有很多面试官都会问到讲讲三种模式的运用和区别,所以自己就来总结一下自己理解的相关的内容。
1、MVC
Model(模型层):主要是对数据相关的操作在Model层中,比如网络请求、数据库相关内容
View(视图层):一般通过xml界面展示出来的与用户交互的UI展示
Controller(控制层):主要是进行逻辑操作的相关内容,Android中一般activity为控制器。
实现:


Android 之设计模式Mvc、Mvp和Mvvm_第2张图片

Controller:

public class OneActivity extends BaseAcvitiy {

    private OneModel oneModel;
    private TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //View的展现
        setContentView(R.layout.activity_one);

        //Model
        oneModel = new OneModel();

        textView = findViewById(R.id.tv);
        textView.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //进行逻辑调用,点击view获取数据操作
            oneModel.invoke(1000, oneCallBack);

        }
    };
    OneCallBack oneCallBack = new OneCallBack() {
        @Override
        public void onSuccess(String data) {
            textView.setText(data);
        }

        @Override
        public void onFial(String errorMessage) {
            textView.setText(errorMessage);
        }
    };
}

View:



    


Model:

public class OneModel {

    public void invoke(int time, OneCallBack oneCallBack) {
        setOneCallBack(oneCallBack);
        //仿网络请求,嘿
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //网络请求状态判断
        if (getOneCallBack() != null) {
            switch (time) {
                case 1000:
                    getOneCallBack().onSuccess("请求成功:" +
                            "Find something you love doing and then find a way to scale it.");
                    break;
                case 3000:
                    getOneCallBack().onFial("请求超时");
                    break;
            }

        }

    }

    private OneCallBack oneCallBack;

    public OneCallBack getOneCallBack() {
        return oneCallBack;
    }

    public void setOneCallBack(OneCallBack oneCallBack) {
        this.oneCallBack = oneCallBack;
    }
}
Android 之设计模式Mvc、Mvp和Mvvm_第3张图片

Android 之设计模式Mvc、Mvp和Mvvm_第4张图片

在MVC中,Activity/Fragment既充当了控制器层,又充当了VIew层,导致耦合性很高,如果项目的需求功能越来越多,Activity/Fragment中的代码会越来越庞大,会导致难以维护和测试。所以就会有了后面的MVP的架构模式的出现。
2、MVP
Model(模型层):同MVC中的Model层
View(视图层):同MVC中的VIew层
Presenter(控制层):主要是进行逻辑的操作,来支持View和Model之间的交互
实现:


Android 之设计模式Mvc、Mvp和Mvvm_第5张图片

Model:

public interface IModel {
    void network(int time,TwoCallBack twoCallBack);
}
public class TwoModel implements IModel {

    @Override
    public void network(int time, TwoCallBack twoCallBack) {
        setTwoCallBack(twoCallBack);
        //仿网络请求,嘿
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //网络请求状态判断
        if (getOneCallBack() != null) {
            switch (time) {
                case 1000:
                    getOneCallBack().onSuccess("请求成功:" +
                            "Find something you love doing and then find a way to scale it.");
                    break;
                case 3000:
                    getOneCallBack().onFial("请求超时");
                    break;
            }

        }
    }
    private TwoCallBack twoCallBack;

    public TwoCallBack getOneCallBack() {
        return twoCallBack;
    }

    public void setTwoCallBack(TwoCallBack twoCallBack) {
        this.twoCallBack = twoCallBack;
    }
}

View:

public interface IView {
    void success(String data);
    void fail(String data);
}

Presenter:

public interface IPresenter {

    void init(IView iView);
    void invoke();
}
public class TwoPresenter implements IPresenter {
    private IView iView;
    private IModel iModel;

    @Override
    public void init(IView iView) {
        this.iView = iView;
        iModel = new TwoModel();
    }

    @Override
    public void invoke() {
        iModel.network(1000,twoCallBack );
    }

    TwoCallBack twoCallBack = new TwoCallBack() {
        @Override
        public void onSuccess(String data) {
            if (iView != null) {
                iView.success(data);
            }
        }

        @Override
        public void onFial(String errorMessage) {
            if (iView != null) {
                iView.fail(errorMessage);
            }
        }
    };
}

Activity调用:

public class TwoActivity extends BaseAcvitiy implements IView {

    private Button button;
    private TwoPresenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //View
        setContentView(R.layout.activity_two);
        //Presenter初始化
        presenter = new TwoPresenter();
        presenter.init(this);

        button = findViewById(R.id.bt);
        button.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //调用逻辑实现
            presenter.invoke();
        }
    };

    @Override
    public void success(String data) {
        button.setText(data);
    }

    @Override
    public void fail(String errorMs) {
        button.setText(errorMs);
    }
}

在MVP模式中,主要的特点是面向接口编程的。它将业务从Activity/Fragment中分离出去,把功能逻辑操作都放在了Persenter层,Activity/Fragment只负责视图层的展示,VIew层和Model层没有任何交互,降低了耦合,更便于后期项目的维护和开发。

3、MVVM
Model(模型层):同MVC中的Model层
View(视图层):同MVC中的VIew层
ViewModel(视图模型层):处理逻辑相关内容操作


Android 之设计模式Mvc、Mvp和Mvvm_第6张图片
image.png

Model层:

public class ThreeModel {
    public ObservableField data=new ObservableField<>();

    public void invoke(int time,IModel iModel){
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        iModel.onSuccess();
    }

View层:

public class ThreeActivity extends BaseAcvitiy {

    private ActivityThreeBinding activityThreeBinding;
    private ThreeViewModel threeModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activityThreeBinding = DataBindingUtil
                .setContentView(this,R.layout.activity_three);

        threeModel = new ThreeViewModel(activityThreeBinding);

        activityThreeBinding.setModel(threeModel.getData());
        activityThreeBinding.setMain(ThreeActivity.this);

    }
    public void onClickTv(View view){
       threeModel.invoke();
    }
}

xml文件:




    

        
        
    

    

        

    

ViewModel层:

public class ThreeViewModel {


    private ThreeModel threeModel;

    public ThreeViewModel(ActivityThreeBinding activityThreeBinding) {
        threeModel = new ThreeModel();
    }

    public ThreeModel getData() {
        threeModel.data.set("请求数据");
        return threeModel;
    }

    public void invoke() {
        threeModel.invoke(1000, iModel);
    }

    IModel iModel = new IModel() {
        @Override
        public void onSuccess() {
            threeModel.data.set("请求成功");
        }

        @Override
        public void onFial(String errorMessage) {

        }
    };
}

在MVVM中,View和Model使用DataBinding来进行双向绑定,一方的改变都会影响另一方,开发者不用再去手动修改UI的数据。项目结构更加的低耦合。

总结:这篇文章主要介绍了三种不同模式的区别和简单的例子实现,具体项目用什么模式可以自己针对具体情况去选择合适的模式。文章中有任何问题,和不对的地方,欢迎指出,后续会继续完善。

你可能感兴趣的:(Android 之设计模式Mvc、Mvp和Mvvm)