DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Stone
Stone

Posted on

Some understandings about MVC \ MVP \ MVVM

Recently, there is a new project that I want to develop with MVVM。So I try to learned about the MVVMοΌ›I found I have some misunderstandings about MVC / MVP / MVVM or have somethings that doesn't have a deep understanding,I sort out the functions of each module.

mvc:
mvc
mvp:
Alt Text
mvvm:
Alt Text

β… οΌšpresenter/ViewModel

Presenter Or ViewModel is used to deal with presentation level logic,otherwise business logic belongs to m layer

Suppose the application has a user information page. This page has two states, one is browsing state, the other is editing state. The state transition is triggered by an edit button. When editing, some information items can be edited. Then there is an presentation layer logic, that is, click the button to switch Browse / edit status.

The view layer needs to do very little, basically accepting user events and passing them to presenter or ViewModel(the view layer is responsible for receiving the click event of the Edit button, and then notifies the presenter / ViewModel), and then the presenter / ViewModel informs the view layer whether to display the view in browsing status or in editing status

public class InformationView {
    void initView() {
        // responsible for receiving the click event of the Edit button, 
//and then notifies the presenter / ViewModel
        editStateButton.setOnClickListener(new OnClickListener() {
            presenter.onEditStateButtonClicked();
        })
        ...
    }
    public void showNormalState() {
        // 
        editStateButton.setText("edit");
        nickName.setEditable(false);
        ...
    }
    public void showEditState() {
        editStateButton.setText("finish");
        nickName.setEditable(true);
        ...
    }
}


public class ProfilePresenter {
    private State curState = State.NORMAL;
    public void onEditStateButtonClicked() {
        // When the button is clicked, judge whether the view should
               //switch the displayed state according to the current state
        // This is the presentation logic
        if (isInEditState()) {
            curState = State.NORMAL;
            view.showNormalState();
        } else {
            curState = State.EDIT;
            view.showEditState();
        }
    }
    private boolean isInEditState() {
        return curState == State.EDIT;
    }
}

the presenter / ViewModel decides what to display according to the current state, and what view does is how to display them

model

The model layer contains business data and operations on business data.

Suppose there is a page for displaying a list of recommended blogs. how do we write it in MVP? Let's ignore that there is no interactive view with the model layer. In addition to handling the presentation layer logic, the presenter also issues business instructions to the model layer. So the presenter does not handle business logic, and the real business logic is still completed by the model layer. The sample code looks like this

public class RecommendBlogPresenter {
    private RecommendBlogView view;
    private BlogMode model;
    public void onStart() {
        view.showLoadWait();
        model.loadRecommendBlogs(new LoadCallback<>() {
            @Override
            public void onLoaded(List<Blog> blogs) {
                view.showBlogs(blogs);
            }
        })
    }
}


public interface BlogModel {
    void loadRecommendBlogs(LoadCallback<List<Blog>> callback);
}
public class BlogModelImpl implements BlogModel {
    private BlogRepository repo;
    @Override
    public void loadRecommendBlogs(LoadCallback<List<Blog>> callback) {
        // BlogRepository.fetch()
                //usally execute on no main thread
        callback.onLoaded(repo.fetch("recommend"));
    }
}
public interface BlogRepository {
    List<Blog> fetch(String tag);
}

The business logic layer is not responsible for getting data , getting data should be at a lower level of the model layer(blogrepository),the model layer only deals with business logic

summary:The presenter / ViewModel relies on the model layer through the interface, and the model layer does not rely on the presenter / ViewModel at all

Top comments (0)

Create an Account!

πŸ‘€ Just want to lurk?

That's fine, you can still create an account and turn on features like 🌚 dark mode.