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