The proposal and origin of the MVVM conceptMVVM is the abbreviation of Model-View-ViewModel. It was first proposed and used by Microsoft. It is a new architecture that evolved from the application method of combining MVP (Model-View-Presenter) mode with WPF. MVVM concept explanation and key points1. Basic Conceptsmvvm1.png
2. Basic points
3. AdvantagesThe MVVM pattern is the same as the MVC pattern. Its main purpose is to separate the view and the model. It has several advantages:
Classic example of MVVM WPFMVVM is not a new concept, and it is currently widely discussed due to its application in mobile development. To explain MVVM, whether from its origin, definition of the concept or its best application, it is impossible to leave WPF. When MVVM and WPF are combined, in addition to meeting the basic MVVM architecture model, multi-mode data and event binding is supported during UI editing (Xaml) to achieve the connection between View and ViewModel, while the use of converters and triggers ensures its flexibility. Therefore, whether you want to have a deep understanding of MVVM or seek to build products in MVVM mode, it is recommended that you carefully study WPF-related development knowledge, which is a good reference. Application of MVVM in mobile developmentWhen using MVVM in mobile development, the view and viewcontroller (Android: file and activity) are officially linked together, and we regard them as a component. The view still cannot directly reference the model, and of course the controller cannot. Instead, they reference the view model. MVVM.png User input validation logic and view display logic processing are usually still placed in the ViewController, but the control of the view should be affected by the data attributes of the ViewModel as much as possible rather than directly operated. Processing such as initiating network requests and data storage can be placed in the Model, but there should not be any reference to the view itself. (Use #import UIKit.h in the model) Presentation logic can be placed in the view model (e.g., mapping the model value to a formatted string), and the view controller itself will no longer be bloated. Apps using MVVM are highly testable; because the view model contains all presentation logic and does not reference the view, it can be fully tested programmatically. In Android development, there are already frameworks like RoboBinding, which can complete the binding work with ViewModel in the layout file. In iOS, it is usually recommended to use MVVM in conjunction with ReactiveCocoa. Goo framework: implementation of MVVM pattern for IOSReactiveCocoa is indeed very popular and highly praised as a responsive concept. From the perspective of learning architectural design ideas, I also like it. However, I am still reluctant to use it in product development. As the saying goes, there are many reasons to like something, but only one reason to dislike it. Reactive is different from traditional thinking and basic IOS development thinking, and the learning cost is something that every team is unwilling to accept. What is Goo?To sum it up in one sentence: Goo is the specific implementation of the MVVM pattern in iOS development. Using Goo for development will not change the existing development habits. In a development, you can consider whether to use Goo based on the convenience of the specific scenario. In short, Goo is small, flexible and easy to use. For exampleGoo.gif As shown in the figure, Goo is displayed by implementing the following small functions. 1: Enter data in the TextField and the Label below will be displayed synchronously 2: Click the button on the left to change the data content, which will affect the displayed content of TextField and Label at the same time 3: Click the button on the right to change the data attribute, which will affect the different attributes of TextField and Label at the same time. Next, let's look at the specific code implemented using Goo - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _vm = [TrialDataVM using]; _vm.text = @"123"; [self.inputText bindingWithProperty:@"backgroundColor" withObject:_vm withDataSource:@"backgroundColor" withBindingMode:TwoWay]; [self.inputText bindingWithProperty:@"text" withObject:_vm withDataSource:@"text" withBindingMode:TwoWay]; [self.hineLbl bindingWithProperty:@"text" withObject:_vm withDataSource:@"text" withBindingMode:OneWay]; [self.hineLbl bindingWithProperty:@"textColor" withObject:_vm withDataSource:@"backgroundColor" withBindingMode:OneWay]; } - (IBAction)clickAction:(id)sender { _vm.text = @"456";} - (IBAction)otherAction:(id)sender { _vm.backgroundColor =[UIColor redColor];} Don't be surprised! It's just a few lines of code. The bindingWithProperty method is used to bind the control to the ViewModel. If you only change the ViewModel later, it will automatically affect the properties of the UI elements. Goo is an open source framework for iOS developed by me based on the MVVM model. I will write a special article to introduce it later, and the code will also be shared on Github. I hope that friends who are interested will join in to continue to improve and promote it. |
<<: Useful information sharing: Let you learn JS closures in minutes
>>: Development and deployment under microservice architecture
The framework is as follows: 1. What is the real ...
Last year, Qualcomm released the flagship SoC Sna...
Author: Liu Yun, Wang Bo, Shi Gongle (Nanjing Ins...
Everyone should have used something with a rose s...
Today’s topic is how to ignite social communicati...
Global mobile phone market landscape Data from th...
Push is defined as the act of a message sender de...
As a video operator platform, iQiyi has developed...
When it comes to redefining information flow copy...
There are so many anti-aging skin care products a...
In my current job, I come into contact with many ...
Not long ago, Tencent released its Q2 financial r...
If you are a fan of American dramas or Japanese a...
Landing page planning has always been a headache ...
The ups and downs of life are too fast, it's ...