MVC, MVP, MVVP mode in Android development

Source: Internet
Author: User

1) Wirelessly development mode, Why use the model ?

Different patterns indicate different code structures and code splitting methods.

The code is written in the classic development mode, which makes our code more streamlined and extensible. Just like building buildings, if the code is misplaced, it may not be possible to build a kennel.

A developer, where should you write the code, and how do I invoke the contents of my schema? Restrict the developer through the schema, the code can not be put in disorder!

Finally, integrate the code together!


The project is divided into modules;

Building the overall project structure;


If you want to embark on management, you must learn some architectural content!


2) What are the common development patterns?

Common patterns: MVC, MVP, MVVC


First, the implementation of user login:

The novice will put all the code in the activity, if the function is very much, it will lead to the code in the activity bloated, which may lead to thread blocking and poor scalability!

What is the benefit of encapsulating the user's input as a users object? is used primarily to determine if the user's input is empty?

The use of the Progress Bar dialog box; ProgressDialog class New ProgressDialog (); Progressdialog.show (); Progressdialog.dismiss ()

How do I display the UI in a child thread? Runonuithread () method

There is a networking operation in the activity, but the networking operation and the operation of the database should belong to the model class!

public class Mainactivity extends Appcompatactivity {private EditText et_username; Private EditText Et_password; Private ProgressDialog Dialog; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Findview (); } private void Findview () {et_username = (EditText) Findviewbyid (r.id.et_username); Et_password = (EditText) Findviewbyid (R.id.et_password); Dialog=new ProgressDialog (this); }//Perform login action//modifier cannot private public void login (view view) {String username = Et_username.gettext (). ToString ( ); String password = Et_password.gettext (). toString (); Encapsulates the user's input into the object final UserInfo UserInfo = new UserInfo (); Userinfo.name = Username; Userinfo.password = password; Boolean B = Checkuserinfo (userInfo); if (b) {dialog.show (); Networking is a time-consuming operation to open a child thread new ThreaD () {@Override public void run () {//Let the main thread sleep 2s systemcl Ock.sleep (2000); if ("111111". Equals (Userinfo.name) && "111111". Equals (Userinfo.password)) {Dialog.dismiss ( ); Note that this is a child thread and cannot update UI Runonuithread (new Runnable () {@Override public void Run () {Toast.maketext (mainactivity.this, "Login succeeded", Toast.length_lo NG). Show (); } }); } else {Dialog.dismiss (); Runonuithread (New Runnable () {@Override public void run () { Toast.maketext (mainactivity.this, "Login Failed", Toast.length_long). Show (); } });}}}.start (); } else {Toast.maketext (mainactivity.this, "User name or password is empty", Toast.length_long). Show (); }}//Verify the user's input private boolean checkuserinfo (UserInfo UserInfo) {if (Textutils.isempty (userinfo.name) | | T Extutils.isempty (Userinfo.password)) {return false; } else {return true; } }}

Second, the MVC pattern in the wirelessly embodiment (the network request to split out , in fact, the results of the network request should be interface callback to the activity):

The code in the activity is split according to the MVC pattern:

Pass the data up, m---->c---->v

Model (Operational database or network)----> control activity----> interface layout

For example, network operations: loginnet What to do in this class: Send the data to the server side and get the results back.

However, the activity does not hold an instance of the Loginnet class, to create an instance of the class in the activity, and to invoke its methods, pass parameters (passing objects, map sets, etc.); The code that is related to the interface is not written in the Network model class ( Activity class to hold an instance of the network request Class).

New Thread () {@Override public void run () {loginnet loginnet = new Lo Ginnet (); Boolean result = Loginnet.sendlogininfo (userInfo); if (result) {Dialog.dismiss (); Note that this is a child thread and cannot update UI Runonuithread (new Runnable () {@Override public void Run () {Toast.maketext (mainactivity.this, "Login succeeded", Toast.length_lo NG). Show (); } }); }else{Dialog.dismiss (); Runonuithread (New Runnable () {@Override public void run () { Toast.maketext (mainactivity.this, "Login Failed", Toast.length_long). Show (); } }); } }}.start (); public class Loginnet {/** * sends data entered by user * * @param userInfo * @return */public Boolean Sendlog Ininfo (UserInfo UserInfo) {systemclock.sleep (2000); if ("111111". Equals (Userinfo.name) && "111111". Equals (Userinfo.password)) {return true; } else {return false; } }}

Third, the MVP model in the wirelessly embodiment:

The disadvantage of using the MVC pattern is that there are two parts of code in Activiy: business-related + interface-related v content is relatively small, but in C more content .

If the business in activity is split: It is the MVP mode, and the business logic in the activity is split.

MVP mode: Want $ The business logic in activity to form a P-layer, and the model layer does not change.

Presenter package, process and business-related code, do not handle too much business logic in activity

public class Mainactivity extends Appcompatactivity implements Iuserloginlistener {private EditText et_username; Private EditText Et_password; Private ProgressDialog Dialog; Private Userloginpresenter Userloginpresenter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Findview (); } private void Findview () {et_username = (EditText) Findviewbyid (r.id.et_username); Et_password = (EditText) Findviewbyid (R.id.et_password); dialog = new ProgressDialog (this); Userloginpresenter = new Userloginpresenter (this); }//Perform login action//modifier cannot private public void login (view view) {String username = Et_username.gettext (). ToString ( ); String password = Et_password.gettext (). toString (); Encapsulates the user's input into the object final UserInfo UserInfo = new UserInfo (); Userinfo.name = Username; Userinfo.password = PasswoRd Boolean B = Userloginpresenter.checkuserinfo (userInfo); if (b) {dialog.show (); Networking is a time-consuming operation to turn on a sub-thread userloginpresenter.login (userInfo); } else {Toast.maketext (mainactivity.this, "User name or password is empty", Toast.length_long). Show (); }} public void Success () {Runonuithread (new Runnable () {@Override public void run () {Dialog.dismiss (); Toast.maketext (Mainactivity.this, "landing success", Toast.length_long). Show (); } }); } public void Fail () {Runonuithread (new Runnable () {@Override public void run () { Dialog.dismiss (); Toast.maketext (mainactivity.this, "Login Failed", Toast.length_long). Show (); } }); }}/** * Created by Administrator on 2016/5/5 0005. * Mainly used to process business logic */public class Userloginpresenter {protected Iuserloginlistener view; Public Userloginpresenter (IuserloginliStener iuserloginlistener) {this.view = Iuserloginlistener; }//Verify the user's input public boolean checkuserinfo (UserInfo UserInfo) {if (Textutils.isempty (userinfo.name) | | Textutils.isempty (Userinfo.password)) {return false; } else {return true; }} public void login (final UserInfo UserInfo) {new Thread () {@Override public void R Un () {userloginnet loginnet = new Userloginnet (); Boolean result = Loginnet.sendlogininfo (userInfo); if (result) {//here how to get to the Mainactivity object, call the method in which the login succeeds with the failed view.success (); } else {view.fail (); }}}.start (); }}

Class userloginpresenter{
Judging user input
Business logic for handling logins

}

In the P class, to get the object of activity, we can invoke the method in activity: Login success and login failure; Obtained through the P class constructor.

Create a P-layer object in activity, then invoke the method in it, declare it without definition, and pay special attention when defining it.

MVP Source Link Address




Four, the MVVP mode of emergence:


MVC, MVP, MVVP mode in Android development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.