Reprinted: Http://kymjs.com/code/2015/11/09/01
1. Why do I need MVP?
What is the most basic thing in software?
A: Yes, it's data. All the code we write is around the data.
The business logic emerges around the change of data generation and modification .
Around the display of data , there are different interface technologies . without well-designed code, it is often the case that the data layer (persistence layer) and the business Logic layer and interface code coupling are present. ORM Frameworks, which decouple the coupling between business logic and data, business logic no longer cares about how the underlying data is stored and read. All the data presented to the business logic layer is a one-to-one object. and MVC, MVP, MMVM is used to solve the coupling between business logic and views .
2. MVC and MVP
(1) MVC
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. where m-layer processing data, business logic, and so on; v-Layer processing interface display results; The C-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. Speaking so much, listening to the feeling is very abstract, not much nonsense to say, we look at the MVC in the development of Android is how to use it!
• Model layer: Suitable for some business logic processing, such as database access operations, network operations, complex algorithms, time-consuming tasks are processed in the Model layer.
The model layer is the core part of an application system, which represents all the functions that the system actually implements. For example: In a video player, the model represents a video database and the program function code that plays the video; In the photo application, the model represents a photo database, and the program function code when looking at the picture. In a telephony app, model represents a phone book, and program function code that calls and sends text messages.
The model is generated in the values directory through the XML file format, or it can be hardcoded directly into Java code. The view and model are connected by bridge adapter.
• View layer: The portion of the application layer that handles the display of data, and the XML layout can be treated as a V-layer, showing the data results of the model layer. View is a feedback result that the software application transmits to the user. It represents the functions of graphical display, sound playback, tactile feedback, etc. in software applications. The root node of the view is the application's own window. For example, the video player may contain the currently playing screen, which is a view. Another view component might be the text caption for the video. Another is a few play buttons, such as: Stop, Start, pause and other buttons.
• Controller layer: In Android, activity handles user interaction issues, so you can think of activity as a controller, Activity reads data from the V-view layer (eg. reads data from the current EditText control), controls user input (input to the Eg.edittext control data), and sends data requests to model (eg. initiating a network request, etc.). Controller in the software application is responsible for the response to external events, including: keyboard percussion, screen touch, call incoming and so on. The controller implements an event queue, and each external event is uniquely identified in the event queue. The framework moves the events out of the queue and distributes them sequentially.
-----------------------------------------------MVC Typical example of the implementation of the ListView-----------------------------------------
The most typical MVC in Android is the ListView, the data to be displayed is model, the ListView in the interface is the view, and the control data is displayed in the ListView as the controller.
• Direct Java code generation by hard-coded way, explained directly in the comments:
1 Public classArrayadapteractivityextendsActivity {2 @Override3 protected voidonCreate (Bundle savedinstancestate) {4 Super. OnCreate (savedinstancestate);5 //the ListView in the interface is view, which is generated by hard-coded direct Java code6ListView ListView =NewListView ( This);7 //control how the data is displayed in the ListView is the controller8arrayadapter<string> adapter =NewArrayadapter<string> ( This, Android. R.layout.simple_expandable_list_item_1, GetData ());9 //The view and model are connected by bridge adapter. Ten Listview.setadapter (adapter); One Setcontentview (listView); A //Click events, controller is responsible for -Listview.setonitemclicklistener (NewOnitemclicklistener () { - @Override the Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) { - //position is starting from 0, get the content of the click item -Toast.maketext (arrayadapteractivity. This, GetData (). get (position), Toast.length_short). Show (); - } + }); - } + //the data model to display, which is generated by hard-coded direct Java code A PrivateList<string>GetData () { atlist<string> data =NewArraylist<string>(); -Data.add ("a"); -Data.add ("B"); -Data.add ("C"); -Data.add ("D"); - returndata; in } -}
• View views and model models take resource file methods:
To create a file Activity_arrayadapter.xml in the Res/layout folder, you can see that only one ListView is included, that is, the views view
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "vertical" >6 <ListView7 Android:id= "@+id/listview"8 Android:layout_width= "Fill_parent"9 Android:layout_height= "Fill_parent"Ten Android:drawselectorontop= "false" /> One </LinearLayout>
Add a character array to strings.xml under the Res/values folder, and model
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3 <String-arrayname= "good">4 <Item>A</Item>5 <Item>B</Item>6 <Item>C</Item>7 <Item>D</Item>8 </String-array>9 </Resources>
Activity code, explaining the MVC model usage in comments:
1 Public classArrayAdapterActivity2extendsActivity {2 @Override3 protected voidonCreate (Bundle savedinstancestate) {4 Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.activity_arrayadapter);6 //the ListView in the interface is View,view generated in the layout directory through the XML file format, obtained with Getviewbyid ()7ListView ListView =(ListView) Findviewbyid (R.id.listview);8 //control how the data is displayed in the ListView is the controller9arrayadapter<string> adapter =NewArrayadapter<string> ( This, Android. R.layout.simple_expandable_list_item_1, GetData ());Ten //The view and model are connected by bridge adapter. One Listview.setadapter (adapter); A //Click events, controller is responsible for -Listview.setonitemclicklistener (NewOnitemclicklistener () { - @Override the Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) { - //position is starting from 0, get the content of the click item -Toast.maketext (ArrayAdapterActivity2. This, GetData (). get (position), Toast.length_short). Show (); - } + }); - } + //the data Model,model to be displayed is generated in the values directory through the XML file format A PrivateList<string>GetData () { atlist<string> data =NewArraylist<string>(); -Resources res =getresources (); - //takes an array of characters in an XML file format -String[] Good=Res.getstringarray (r.array.good); - for(inti=0;i<good.length;i++){ - Data.add (Good[i]); in } - returndata; to } +}
The Android MVC model needs to be understood in the project so that it can be understood thoroughly and ingenious.
Advantages of MVC:
• Low coupling resistance. 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.
• 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.
• module responsibilities are clearly divided. The main partition layer M,v,c three modules, which facilitates the maintenance of the code.
(2) MVP
In UI mode and MVC, one of these is the Model-view-presenter (MVP) mode, which optimizes or replaces the MVC pattern .
Figure 1
Figure 2
The two images are different, but the idea of an improvement to MVC is the same: cut off the view and model links, let view only interact with presenter (original controller), Reduce the number of objects that need to be maintained in a change in demand.
This approach is in line with our expectations, as we tend to:
• solve problems at a lower cost
• solve problems in a way that is easier to understand
many times not a model is bad, but because people can not be implemented, such as not easy to understand, we will choose easy to understand the way.
3. How do I build an app from the architecture level to help him cope with changing interfaces and business logic? Let's talk about a way to implement MVP mode on Android today.
Let's talk about a way to implement MVP mode on Android today. It's also a new architectural pattern that I've summed up in the project, and you can view my THEMVP project: Https://github.com/kymjs/TheMVP
Android Advanced Note 07: Developing Android Apps with MVP Architecture (MVC and MVP)