Recently, more and more people have started talking about architecture. So have my colleagues and engineers. Although I don't have a deep understanding of MVP and DDD, we decided to build our new project through MVP. This article is a summary of my research and study of various articles and special discussions. It includes the following points:
Unfortunately, this article will not cover:
***, I will tell you how to study these topics further. By the way, I gave a talk about MVP architecture at a local seminar last week. This post is pretty much the same as the talk. Introduction ~ Activity is the God class ~ First, let's think about why a clear software architecture is so urgent in Android development. This paragraph is excerpted from "Code Complete Second Edition": Avoid creating god classes. Avoid creating omniscient, omnipotent god classes. If a class needs to spend time retrieving data from other classes via Get() and Set() (that is, it needs to go deep into the business and tell them what to do), then these functions should be better organized in other classes rather than in the god class. (Riel 1996) God classes are expensive to maintain, hard to understand what is going on, and difficult to test and extend, which is why it is a golden rule to avoid creating God classes. However, in Android development, if you don't consider the architecture, the Activity class will tend to become larger and larger. This is because Android allows Views and other threads to coexist in the Activity. In fact, the biggest problem is that business logic and UI logic coexist in the Activity. This will increase the cost of testing and maintenance. Activity is God This is one of the reasons why a clear architecture is needed. Not only will it cause the Activity to become bloated, but it will also cause other problems, such as complicating the life cycle of Activity and Fragment, as well as data binding. What is MVP? MVP stands for Model, View and Presenter.
The following diagram is one of the patterns based on MVP architecture. View is the UI thread. Presenter is the adapter between View and Model. UseCase or Domain is in the Model layer and is responsible for getting or loading data from entities. The dependency rules are as follows: The Dependency Injection The key is that the high-level interface does not know the details of the low-level interface, or more accurately, the high-level interface cannot, should not, and must not know the details of the low-level interface, is (oriented towards) abstraction, and is detail-hiding. The higher interfaces do not know about the details of the lower ones Dependency rules? Uncle Bob's "The Clean Architecture" describes what the rules for dependencies are. Concentric circles divide the software into different areas. Generally, as the level goes deeper, the level of the software becomes higher. The outer circle is the implementation mechanism, and the inner circle is the core strategy. Here is the summary of the above article: Enitities:
Use Cases
Presenters Controllers
External Interfaces, UI, DB
MVC, MVP or MVVM? So which one is the best? Which one is better than the others? Can I choose just one? The answer is, NO. The motivation behind all these patterns is the same. That is how to avoid cluttered code, make it easy to perform unit testing, and create high-quality applications. That's it. Of course, there are more than just these three architectural patterns. And none of them can be a silver bullet. They are just one of the architectural patterns, not the only way to solve the problem. These are just methods and means, not the purpose or goal. Pros and Cons OK, let's get back to the MVP architecture. We just learned what MVP is, discussed MVP and other popular architectures, and introduced the differences between MVC, MVP and MVVM. Here is a summary of the pros and cons of the MVP architecture: **profit
**Cons
show me the code!!! This is just a small section of the MVP pattern. If you want to see more projects or live code examples, please refer to the "Links and Resources" at the end of the article. There are very rich and cleverly designed examples, most of which are hosted on Github so that you can clone, run on your device, and understand how it works. First, define the interface for each View.
Let's rewrite the TopView class. The key points are as follows:
This is the Presenter class. The most important thing is that Presenter is just an adapter bridge connecting View and Model. For example, TopUseCase#saveCalendarDate() hides the details of TopPresenter, and the same is true for TopView. You don't need to care about the data structure or how the business logic works. Therefore, you can perform unit testing on TopUseCase because the business logic is separated from the view layer.
Of course, you can still perform unit testing even if the business logic is implemented in the Activity class, but it will be time-consuming and a bit complicated. It may take more time to run the App, instead, you should take advantage of the performance of the testing library, such as Robolectric. Summarize There is no silver bullet here, and MVP is just one of the solutions. It can be used in conjunction with other methods and can also be selectively used in different projects. |
<<: Mobile development fifth: 6 most popular positions for new programmers
>>: Use Retrofit+RxJava+MVP to create a Material Design style APP
In 1899, a Qing Dynasty official named Wang Yiron...
The B station that everyone usually refers to is ...
【51CTO.com Quick Translation】An Internet browser ...
A recent research report shows that weekends are ...
There are two things that left a deep impression ...
Doctor, I have "little wings" growing i...
As the gossip center of the entire Internet, Weib...
The ancient scroll text that "disappeared&qu...
Training course content: In fact, most people wil...
One day, our editorial group shared a news about ...
Affected by the sharp drop in international oil p...
Author: Zhang Yu, researcher at the Chinese Cente...
Nowadays, everyone on Douyin will try to get traf...
Software Information Name: Weisou Package name: c...
Notice from the State Drug Administration on 22 b...