Use of the Android MVC pattern

Source: Internet
Author: User

The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction.

Model layer processing data, business logic, etc.;

The display result of the View layer processing interface;

The controller layer acts as a bridge to control the V-layer and M-layer communication to achieve the separation view display and the business logic layer.

Case:

 Public classMainactivityextendsActivityImplementsImplement,onclicklistener {PrivateTextView Tv_view; PrivateMainmodel Mmodel; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.th);    Initview (); }    Private voidInitview () {//TODO auto-generated Method StubMmodel =NewMainmodel (); Tv_view= (TextView) This. Findviewbyid (R.id.tv_view); Findviewbyid (R.ID.BTN). Setonclicklistener ( This); Findviewbyid (R.ID.BTN1). Setonclicklistener ( This); } @Override Public voidLoadData () {mmodel.loaddata (Tv_view); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btn:loaddata ();  Break;  CaseR.id.btn1:httpdata ();  Break; default:             Break; }} @SuppressLint ("Handlerleak") @Override Public voidHttpdata () {Object data=Mmodel.httpdata (); if(data!=NULL) {Tv_view.settext (data.tostring ()); }Else{onError (); }} @Override Public voidOnError () {//TODO auto-generated Method StubToast.maketext (Getapplicationcontext (), "Failed to load data", Toast.length_long). Show (); }    }

 Public class Mainmodel {    publicvoid  loaddata (TextView tv_view) {        Tv_view.settext (" MVC pattern in Android application, model get Data ");    }     /** *     * Network request     *      @return  return request data      */public       Object httpdata () {        return "network request returned data";}    } 

Advantages of MVC:

1. Low coupling. The so-called coupling is the degree of association between module code. With the MVC framework, the view layer and model layer can be separated very well, so as to realize the purpose of decoupling, so the coupling is low and the interaction between module code is reduced.

2. Scalability is good. Because of the low coupling and the need to add, the extension code reduces the number of changes to the previous code and reduces the incidence of bugs.

3. Module responsibilities are clearly divided. The main partition layer M,v,c three modules, which facilitates the maintenance of the code.

SOURCE Click to download

Use of the Android MVC pattern

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.