Next, this article began with Android to explain the concept of MVP, stereotyped-style shelf structure and naming conventions. In preparing this article, I also saw a lot of ideas on the basis of the MVP, the Foundation is the MVP right, but the name of the difference, the complexity of the change, the shelf is also used in the module split and not simply with the package to split, so next will be based on the Googlesamples recommended naming, Frame structure to reconstruct my app, I will pull a new branch to correspond to each chapter, and then I will continue to refactor in a moderate, practical direction that I think is appropriate.
sticked Avoid excessive! . Sticked is to isolate the code, what to do (plainly, is to read the data to put a folder, the interface put a folder). Now the popular "domain", "repository", "Service" and so on named, you if the. NET Monk must smile: "and TM to force"-these naming and early "module", "Biz", "helper" The name is the same (you can go to the Sophie forum to see what the great God wrote a few years ago, which is also very useful today.) NET Universal Library).
When you think you don't understand, it's time to close your hands . I would like to emphasize this point again, for example, I read this clean-architecture and this Mosby in order to prepare articles. A face of the Meng, not because the birds are poor, I can not be laborious to read, but because I am looking at the time of my mind has been deep feeling "I need to do this?" , after I suddenly understand that Canton point 2 cents income is not because of poor shelves, but because crash too much, ha ha. See don't understand also want to continue to install force, like just open furnace hood to ponder how to carry tens concurrency as silly. You just have to remember which folder to put the code file, what to do with this layer, it is good enough.
By the way, in the back of the iOS, I will talk about the MVC pattern how to combine the idea of sticked, just also for the article to the point of the power marathon has always wanted to do the iOS end do (because to now use wide-point revenue only 2 cents do not want to get, haha ha). iOS is now scrambled with MVVM fire very, about the MVVM pattern I was very rejected, in the early days of Silverlight used to feel too troublesome (this is also my point of view, not excessive), data binding this matter, logic is simple OK, to do a lot of other work, You just have to write down the base code of the shelf to toss it. In the back of the PHP side (Laravel), I will talk about the folder how to put, file name how to take it is very professional, with a great and elegant Laravel framework to summarize the next article.
Articles and items worth reading
Since Weibo has focused on a lot of great gods and several development blogs, various technical articles have been blown on screen. Especially like me this curiosity wins, the direction of the attention is many, looked very depressed ah. Here I first give a few I think you should study the article and project, than I write good, write vivid (star Mark refers to I think of reading friendship).
- Google Flavor MVP Practice ★★★★
- Android: How do you write the activity so "omnipotent" ★★★★
- Android: "Magnum" activity refactoring article ★★★★★
- Mvp+dagger2+retrofit to achieve a clearer architecture ★
- The zero-based Android new Project 6-repository layer (under) Realm, cache, exception handling ★★★★ (this series is highly recommended to watch, the great God also put a no confusion no reinforcement has been launched APK let you reverse, moved)
- Architecting Android ... The clean? ¡ï ★
- Architecting Android ... The evolution★★★
- the-clean-architecture★★★
- Model-view-presenter Library for android★★★
Items to discover new will edit add:
- MVP Class Code ★★★★★
- Crew (reverse look, note dagger2 will add a lot of support code) ★
The vernacular MVP model
In the process of learning shelves, various translation differences and understanding errors caused a lot of trouble, what is "business logic"? "Domain Layer"? "Data map layer"? Very annoying there is no, especially the early online article authors are the Great God, think the masses are genius, also do not give some examples of the extremely obscure description with the past. Fortunately, many of the great gods have discovered this and have begun to describe these concepts in vivid and lightweight language. The article recommended in the previous believe that you also absorbed almost, I will be the actual details again in the Vernacular MVP mode (I do not have the old driver belt, understand the deviation please correct).
Are you like me?
Have you ever put a lot of data entities in a package called data or beans or model? such as "UserBean", define the user's name, gender field, add Get/set method, Persistence (parcelable), with Gson or ORM will add @serializedname, @Table and other injections.
Where you define the data
Next you will be the network access to which URL to obtain user data code written in a package called Manager or API or network, JSON data is read from the network and converted via Gson you will be written in the fragment or activity file where the ListView is located (for example, Loadnewdata).
Where you define the network access
You convert network access data to a ListView where data can be read
If you are a person, then you will write the logic of the SQLite operation and put it in a packet called DAO or DB, and write the data into SQLite to save it. Every time you read or write from the database, it will be executed with asynctask (maybe you will create a new package called task or the fragment or activity file where the ListView is located, but I didn't get it)
If you are holding a ticket to make the function more robust, the code that uploads the data is written in the service, then a service package is added, and you need to add a notification, then you will create a package called boardcase and notification, a storage broadcast, A storage notificaition; the functions of image processing and time string processing are written in a bag called utils; the third-party wheel view Control is written in a package called a widget.
Did you find anything wrong?
Write code for a "logical separation", simple to read and write data in a heap; interface interaction (including data display) together, the advantage is easy to maintain, easy to expand (project small not this experience?). That in the hard disk, the code put a folder, UI design material put a folder, the document put a folder, to use the time directly open which folder in mind, so understand it.
That other article always mention "business logic", "Functional Logic", how to distinguish it. To use the login function for example:
- You use the phone number or login, verify the format or by obtaining tokens and userinfo, is to implement the login function, but the method is different, you can call Umeng or third-party native SDK, for these write code, calculate business logic.
- Send the user registration information to the server to save, and receive server feedback (such as to keep the user login status token), stored in the preference or database, the code calculates the business logic.
- How to put the data into preference or database, is nothing more than adding and removing changes, then this part of the code even if the function of logic.
code written to implement specific business functions is called "business logic." Log in to get personnel location, SMS verification code is counted. And, you need to modify the server feedback before you deposit data (such as from the user gender is M, F, you want to save as 0, 1), this code is also counted into the business logic. In all kinds of shelves, "domain", "service", "repository" are all these things.
to implement a reusable function code, called "Functional logic." Whether the business is logged on or off, want to access the network or read the database, and finally call the platform method, then how to invoke the Platform method code, is the function logic. such as access to the network, whether you want to post login URL or post logout URL, will be used to call the Android network access code, such as additions and deletions, whether you are preference or sqlite, the end will call the Android method to execute; array conversions, Temporary file management, picture compression, check whether the string is empty, MD5 encryption, time formatting, and so on, you can write a package called Utils to save the code. Boardcase, receiver, Sevice, notifacation should all be separated by the package unit.
A lot of wheels have been done for you, such as reading a picture of the glide or network access to the Okhttp
According to the idea of subdivision, then it is obvious that the framework of my app in the previous section, although the results of the package is clear, but the reading is a mess, and it is really "read" a mess, such as the index of the fragment code line is too high, I have to simply "move" the code To a baseindexfragment, it is easy to read the code that separates the data read from the interface interaction. MVP mode is a good solution to my unprofessional separation scheme, provides a standard template, what code should put where.
The model layer is no longer just a data entity.
According to MVP, the model layer is to put all the data related to the code here, that is, to define the data entity, the network read JSON and converted to an array of data, the database read and write data are all written here. Presenter layer want to get data, that must have been processed not need to deal with the data, how to obtain data (from the network or database), the data how to convert, all do not tube.
Data processing all the shift model layer
Where does the project manifest itself, in the fragment where the ListView is located, the code that gets the JSON from the network and transforms it into the data array operation is moved from the original Loadnewdata function to the Xxremotedatasource file in the model layer. The Asynctask code that gets the data from the database is moved to the Xxxlocaldatasource to determine whether the code that displays the local data or the network data is moved to the xxxdatarepository. In this way, the code in the fragment a lot of refreshing, is not feeling good? We continue to look at presenter.
The presenter layer is like an elevator.
What should I do before the data is read and read? Click the login button after the server returned to the login status of the jump home or prompt password error? These things are presenter dry: The model layer is prepared to display the data on the interface, there are interactions to remind the model layer of data needs to be updated. Like an elevator, people come in, to massage to the 3 floor, to KTV to 4 floor, to open room to 5 floor. Come in is a male or female you do not need to care, why go out and you do not need to care.
The interface (interface) definition behavior is used extensively in the presenter layer. Used to be no interface, the first trouble, the second family is a team so convenient test, I only do violence test not so fastidious. Now or honestly use it, if not used to you can first realize the function, and then reconstruct the interface, or the commonly used additions and deletions to the first copy into use, slowly habits.
I'm going to start writing the interface.
At this point you may have some confusion, if there are related to such as IM communication, Baidu lbs positioning such functional logic code, where to put? The code may need to depend on the context (that is, the constructor wants context or activity, which you must have seen), and I think it should be put into the utils package. The code that passes the parameter, the state succeeds, or fails is put into the presenter. That is, the interface-related code is placed in the presenter, the other by business logic or functional logic can be categorized as far as possible refactoring.
The view layer does not refer to the control
The view here is thought, not a specific button control or list. You can fill a picture: View and presenter on the bed (fragment or activity), one will viwe on top, one will presenter on top.
View is the one that lets you in the notification interface which should be hidden, which should refresh the
From a simple news list to a complex point with a carousel banner, a multi-level drop-down menu, you can express the State and interaction of the control in the view layer, notifying presenter that the control is clicked to do what, or Prenseter notifies that the control should be hidden or displayed. The view layer in the MVP structure, instead of writing a lighter piece of code, as long as the model and presenter write well, tomorrow on GitHub to see a more cool wheel or want to add an animated effect, is very refreshing.
Talk about something else.
I believe you have read my recommendation of the articles, then I this very vernacular to the MVP model, with a lot of usual will use the details to explain, should be more profound understanding of the concept. So let's talk about something else before I finish writing the next article on my project update.
Which one should rxjava and handler use?
Do you also like to write your custom handler in the fragment file? or Asynctask, or adapter. The function is simple point of the problem is not big, so write fast and convenient, reading is also possible. But if you encounter the login function, first through the Umeng to obtain the user information, and then successfully obtained from the server seven cattle upload token, and then transfer pictures, image upload successfully put the Avatar address and user information post to the database, successfully from the server to obtain the token Store app database. Like this "ring set of Ring" business code, draw time series diagram are very troublesome, with handler can write, but after a while to read it is very unhappy.
Frequent network access can make handler bloated
Rxjava flow programming ideas can be a good solution to this problem, although the amount of code is not necessarily less, but it can make reading a bit happier. As for the network module you use Async-http-client or okhttp or volley, or the recent very fire retrofit2, this is to see yourself. I've been using async-http-client, old wheels, and I don't feel anything wrong (maybe my network access rules are too simple). In addition you want to be a technology network red, RXJAVA+RETROFIT2 is the necessary artifact, but to. NET monk, or hehe bar, there is no need to fry to this height.
There must be something wrong with my article.
I don't have an old driver's belt, all on GitHub and reverse engineering. Sure there will be problems, please also found the correct, thank you very much! QQ Group 533838427
App Shelf Migration Guide (ii)