directory

MVP stuff (1)… Speak with the scene

MVP stuff (2)… A Preliminary study on MVC Architecture

MVP stuff (3)… Using MVC in Android (Part 1)

MVP stuff (4)… Using MVC in Android (Part 2)

MVP stuff (5)… The relationship between the mediator model and MVP

MVP stuff (6)… MVC turned into MVP

MVP stuff (7)… Repository Design analysis

Why MVC first?

If you want to better understand MVP, and in the actual development of flexible application, then it is necessary to understand its low version of MVC, they are just one step away, first understand MVC and then learn MVP, MVP advantages can be highlighted, so that the continuous learning will deepen the understanding of MVP.

As we mentioned in the previous chapter, MVP (2) use scenario to talk about MVC design for our case, which is the first step, how to implement an MVC. Before implementing it, we should first clarify some concepts.

What does MVC do? Is it a design pattern? Is it a frame? Is it architecture?

MVC is a member of the layered architecture, whichever layer from the reusability and extensibility are restricted by the current business, if the beginning from the reuse and extension as a starting point to use the MVC, must have enough development experience (not can’t, layered architecture is also can have extensibility, but want to consider to primary and secondary). Unless you are very clear about the complexity of future software. For example, let’s say we have an algorithm that takes the following relationship to process a set of data: Article 10, need 1 second, 20 takes 1.8 seconds, 30 of them takes 2.5 seconds,,,, 100 to 6 seconds, what can be seen that the time complexity, the greater the amount of data that can be seen that this algorithm processing, its processing time consumed increases more slowly, and the more large amount of data can reflect the value of it, the more of an actual example again, If two people go to a restaurant with 100 per capita, each person may only taste 2 to 3 dishes, but if four people go to a restaurant, each person will taste 4 to 6 dishes, and 20 people go to a restaurant, you may be able to taste all their dishes. The same is true for MVC, the higher the complexity of software, the more the extension value of MVC can be expressed. That’s why IT’s important not to start with extensibility as a starting point for MVC, unless you’re an experienced person who can estimate the complexity of a project in advance. Safer way should be in the project of promoting continuously optimize their structure, many of the new into the erroneous zone, a look at the MVP everyone in use, design a yourself, to finally found that there is a person you in eat food, more crucially others want to eat with you and can’t eat, not general, cannot reflect its extension value, after officials discovered the first reaction was, What are you doing with so many interface abstract classes? It’s just for you. It’s for you.

MVC is not a simple design pattern

Can be called a only for layered design pattern, then the appearance looks like is not the most awkward, so it can’t simply call for design pattern, because it is the scope and depth of more stereo, known as the architecture is more appropriate, of course, can also be called composite design pattern, it contains inside, strategy, combination, observers, such as design pattern. So what is the relationship between architecture and framework?

Architecture and framework are not the same thing

MVC is an architectural idea, and the framework based on this idea is also called MVC framework. In the development of ios, the system provides us with a public view class UIView, a controller class UIViewController, and the famous Struts2 framework, A framework is a bounded extensible set. It is both extensible and bounded, meaning you can only play within the framework, not outside the framework. If you only use MVC ideas for layering in your project, then the project or module is designed with MVC architecture. If you use the Struts framework, then the project uses MVC framework. My project uses the MVVM Architecture Components as the framework for development. Hello everyone, I’m going to introduce an Architecture Components based on the MVVM framework, which sounds awkward.

To sum up — Architecture is a blueprint, and frameworks are real products. There is only one MVP architecture, and there are thousands of frameworks based on MVP architecture.

The above is a general introduction to MVC. Before learning MVC, you should first know how to write. After learning how to write, you can learn how to use it.

Implement MVC what? MVC (Framework)

In the scenario in Chapter 1 we pick a requirement, and we implement a requirement that requests data from the server and displays it in a list.

public class TasksActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { //... Data Data = loadData(); } void onDataCallBack (Data Data) {// upDateList(Data); }}Copy the code

This is a simple example of code that normally uses a method like loadData to request data and bindings to listen for the data to be returned. When the data is returned, a callback method like onDataCallBack will be triggered and the UI will be updated.

Same requirement, write it again in MVC

MVC is Model, View, and Controller. As the name implies, first we need to create these three objects. Second, we need to combine these three objects.

What is a Model and what does it do

Logic layer (Domain layer) Domain Object, logic implementation body, in addition to also can take into account the duties of Javabeans, but the Model layer is not a simple Value Object. Javabeans, also known as Value objects, are only used to encapsulate data and do not have the responsibility of the Model layer. Of course, javabeans can also be used as an internal class of the Model class. Of course, they can also be used as a separate class. Of new ⚠ ️

1. Javabeans are not models, but models can contain javabeans’ responsibilities, but they do not have to. Model is used to process data, such as fetching data (local or server-side), and data processing, such as CURD.

Let’s write a Model based on the above definition

Public class TasksRepository {// getTaskCache() {// getTaskCache() {// getTaskCache() {} // Get Data from disk cache Data getTaskDiskCache(){} // save a Data Boolean saveTask(Task Task){} // Sort Data Data orderData(Data Data, int orderType){} }Copy the code

TasksRepository contains five methods that, as defined above, are responsible for fetching Data. Therefore, the three methods are related to data acquisition, such as getTasks. You can call network components to get data, and it can also perform CURD operations on data. For example, savaTask is used to save a data, and it can also conduct business processing on data. For example, reorder the data using the orderData method.

In short, the Model is responsible for getting data, manipulating it, and doing business with it.

What is a View and what does it do

So let’s talk about views, which is our View layer.

1. Its main responsibility is to present the data of the Model and actively inquire about the status or passively monitor it. 2

Let’s design this class according to View’s responsibilities

Void viewCreate() {controller.loadNomData(); // Public class TaskView {// When the list is initialized, tell the controller to load the data. } / / update list void upDateList () {/ / active request Model to get the Data Data Data = tasksRepository. GetTaskCache (); // Update UI list.update(data); } void beginLoadData(){ list.showHead(); }}Copy the code

In TaskView, which is our view object, it contains two objects, controller and taskeRepository, controller is our controller object, we’ll talk about that next, taskeRepository is our model, we mentioned that, So let’s not worry about how these two objects are initialized, let’s just worry about the View object which contains C and M, which it’s obligated to do, First, methods like viewCreate are called during interface initialization (on Android, it can be an Activity or Fragment initialization, or it can be an execution action), and let the Controller execute the loadNomData method to request data. The only thing to note here is that the controller is not explicitly told where the requested data is coming from, whether it’s a cache or a network request, and the View doesn’t need to care about that. It should also be noted that the Model is responsible for fetching data, but in the above example, ** why is the controller responsible for fetching data, instead of using the Model? Our tasksRepository object? ** Please keep this question in mind and answer it when we talk about controllers.

The second method, upDataList method,

/ / update list void upDateList () {/ / active request Model to get the Data Data Data = tasksRepository. GetTaskCache (); // Update UI list.update(data); }Copy the code

Internally, the tasksRepository object gets the data from its getTaskCache() method, which you’ll remember from the Model definition. At this point, * * you may find, there is a serious breach of the view in the execution tasksRepository. GetTaskCache (), how do you know the data is ready? View responsibility 1:

Actively ask or listen to the Model

We call tasksRepository. GetTaskCache () as active inquiries, and before the active inquiries, the View should be a valid notice, the notice should be initiated by the Model, when listening to the Model: So when MY data is ready and you come and get it, the View is going to fetch the data from Model. A real-life example, you buy things on the Internet, now generally to express compartment, wait until the message inform you Courier to you to express compartment with goods, but maybe you can’t wait, also can call every day, we won’t discuss in detail, the relationship between the View and the Model but also is the relationship between the observed &observer. However, in the example above we did not write code to listen to the Model, please keep this question in mind, I will explain the dependency binding of the three later. The last method, beginLoadData(),

void beginLoadData(){
        list.showHead();
}
Copy the code

When you start requesting data, the list will display its own header layout, and methods like beginLoadData to edit the view itself are a lot more common in actual development. They focus on the state of the control itself, and most importantly, such methods can be called by the Controller at any time. We’ll talk about that when we talk about controllers.

What is a Controller and what does it do

The Controller, our Controller, sends the view’s actions to the model.

Receives the View’s actions and transfers them to Model to change the View’s state

In accordance with the above responsibilities, we try to design this class:

/ * * * * / I am a Contorller public class TasksController {void loadNomData () {if (tasksRepository. GetTaskCache () = = null) { // Execute Modle TasksRepository.gettasks (); // execute View view.beginloadData (); }}}Copy the code

Before explaining the Controller, let’s pause for a moment. In the previous introduction, we seem to have left some problems. When introducing the View, I marked three problems with the way of rent text. Let’s review these two questions:

In the example above, why is controller.loadnomData () responsible for fetching data instead of using Model? Our tasksRepository object?

The problem starts with the Contrller’s job: to take the view’s operations and transfer them to Modle. What are the actions of a View, such as a drop-down refresh of a list, transferred to the Model, which means that the Controller doesn’t actually have the code to load the data, but the Model does it. After explaining this, Take a look at the first method loadNomData() in TasksController,

Void loadNomData () {if (tasksRepository getTaskCache () = = null) {/ / execution Modle tasksRepository. GetTasks (); // execute View view.beginloadData (); }}Copy the code

The first statement inside it:

tasksRepository.getTasks();
Copy the code

The tasksRepositor is our Model, which does the getTasks() task, but the Controller loadNomData() method just encapsulates the process. After listening to the above explanation, we know that Controller is designed in this way, but another problem arises. Although we know that Controller has changed the method of Model, why should I do so? Tasksrepository.gettasks (); Can’t you? Keep this question in mind and I will answer it in the next chapter. Let’s learn how to write first and then study how to use it.

Question 2: There are many methods such as beginLoadData to edit the state of the View itself in the actual development process, they are purely concerned with the state of the control itself, most importantly, such methods may be called by the Controller at any time.

So let’s just review this method beginLoadData()

// The controller will call void beginLoadData(){// Display the head of the list, changing the list property list.showhead (); }Copy the code

The Controller has the right to change the state of the View, either in the form of notification or direct access. Moreover, the View’s responsibilities also indicate that it can receive the Controller to change its state, and the more important point is that the state change does not depend on the Model, which is more pure.

The loadNomData() method in TasksController checks whether the cache can be retrieved from tasksRepository. If not, getTasks(), Notifying the view that I’m about to start loading data, the view will perform a display header on its list control in the beginLoadData() method, which is quite a standard wrapper.

** Summary: ** At this point, we have studied the definitions and responsibilities of the three layers of MVC. In the beginning of the article I have said before, if you want to use the MVP, will use the MVC first, if you want to use the MVC is can “write” on using MVC, writing is the first step, we first put the MVC three layer, split into three segments, each segment specification to their job well, then we could think of some way to put it in the assembly together, in the next chapter we must solve the following problems:

1. How to put these three pieces together? How do I write it in Android? . 2, the view in the execution tasksRepository getTaskCache (), is how to know tasksRepository data is ready? How do YOU tell the view?

MVP stuff (3)… Using MVC in Android (Part 1)

Thank you for your attention and comments. The update is a little slow. Why not follow me and know the update progress at any time?