Pattern is the replication application of empirical knowledge. MVC design pattern has different elaboration and application in different development platform. The Java version, C + + version, C # version, and iOS version are now available on the web. I also publish this article about the MVC design pattern, and use my edge to go your way.
write it in front.
If you do not use design mode, can not develop the design program? Or. So what does the design pattern bring to us? If you do not learn the design patterns summed up by others, you can easily, quickly, really solve the problem, but also willing to do it again, I believe you do not need other people's design mode. If?? Some problems, so you are very scratching your heads, so that you dare not look back, may wish to use other people to summarize the design patterns, to improve your solution.
In the design works, if in order to use the design pattern and apply the design pattern, I think a bit of dazzling technical suspicion, can not do. In the iOS app development process, data processing and UI rendering are generally involved. There are a number of drawbacks to working with the UI when calculating data: 1. Easy to disperse energy. 2. Not conducive to reuse. 3. Redrawing the screen easily results in repeated calculations. In order to solve these shortcomings, we need to decouple, MVC design pattern is symptomatic medicine.
MVC Design Patterns for iOS
What is the MVC (Model-view-controller) design pattern? Let's look at the diagram below.
This is first and foremost an understanding of classification. For example, what is there in a program page? Easy to see the data content, such as some text description, some image content, there are data related to the presentation, such as text color, background color, image size, location and so on. Is there anything else? I'm afraid a static page is hard to hook up with this answer: rules, logic, Tao. These are the program logic and decisions that are hidden behind the program pages, between the data and the presentation.
The program page contains:
1. Data
2. Presentation Mode
3. The relationship between data and presentation mode.
For example, I have four cards to put on the table. How to put it? Are they fan-shaped or are they arranged in a row or in a row? The four cards are the data, the fan arrangement is the presentation method, the desktop is our program page. This "how" Behind the program page is the hidden decision logic.
We can call these three three different duties. These three responsibilities can be applied at different levels of thinking, such as system architecture, software architecture design, and programming. As long as we consider these three aspects, we apply the MVC design pattern. Of course, at the level of the program code, iOS library in the name and responsibility of the class are very clear, such as Uitableviewcontroller,uitableview,uitableviewcell,nsarray,nsnumber and so on. We can also make a good name when designing our own classes, and each class focuses and focuses only on one of the responsibilities.
The MVC design pattern is a product of this thinking pattern. Contains the following content:
1. Model <=> Data
2. View <=> Presentation mode
3. Controller <=> Relationship between data and presentation mode
4. The interaction between the above three. What's the relationship?
1. Model does not speak directly to the view, two goodin lines between the model and the view.
2. Model and controller call, medium model and controller in the two gray virtual line, among the green arrows, the Model on the Orange kvo.
3. View and controller call, between the controller and the view of the two gray virtual line, among the outlet green arrows, yellow delegate and data source.
4. The action on the view and the target on the controller.
5. The KVO on the model on the transmitter and controller on the yellow receiving end.
Specific steps:
When designing the model, focus on the properties that the data has.
When you design a view, focus on the rendering of any one view, not the data presented in the view, as needed from the controller to get the line.
When designing a controller, focus on how to get the data from the model, and how to notify the view to present the new data. Communication with view can be done by defining the message with protocol, which tells the Controller the details of its event handling through the delegate, and the view uses the data source to ask for the information that needs to be rendered.
Example: We can use the above example to place a card. By imitating Uitableviewcontroller, each card is designed like a Tableviewcell class, the entire desktop is designed like a TableView class, and the logic is not designed to resemble a Tableviewcontroller class. The content of each card is designed to be a class inheriting from NSObject, which contains attributes such as names, pictures, etc. With this framework, you can put any number of cards to the desktop, any different cards, you can arrange the cards in any different way. reusability, scalability, low coupling and other quality attributes are available. To display the page:
The gossip is complete. Thanks to the readers, I hope to bring you inspiration.
Gossip about the MVC design pattern for iOS