Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mobile architecture problems and solutions.

237 views

Published on

Mobile architecture problems and solutions.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Mobile architecture problems and solutions.

  1. 1. Mobile Architecture Chris Jimenez
  2. 2. Mobile Architecture En el principio de los tiempos...
  3. 3. MVC
  4. 4. Cons ◦ Massive View Controller! ◦ Sometimes hard to test ◦ More “layers” are needed as app grows ◦ Its an “Apple thing” ◦ Decrease code clarity ◦ There are better design patterns
  5. 5. What better you say? MVVM - Model View View Model --iOS MVP - Model View Presenter --Android
  6. 6. MVVM ◦ View controllers are technically distinct components, they almost always go hand-in-hand together, paired ◦ When is the last time that a view could be paired with different view controllers??
  7. 7. MVVM So why not formalize their connection?
  8. 8. MVVM ◦ Most of the stuff in ViewController is presentation logic ◦ Things like transforming values from the model into something the view can present
  9. 9. MVVM MVVM to the rescue
  10. 10. MVVM MVVM is an augmented version of MVC where we formally connect the view and controller, and move presentation logic out of the controller and into a new object.
  11. 11. MVVM Why? ◦ Reduces the complexity of one’s view controllers and makes one’s presentation logic easier to test
  12. 12. Some Code @interface Person : NSObject - (instancetype)initwithSalutation:(NSString *)salutation firstName:(NSString *)firstName lastName:(NSString *)lastName birthdate:(NSDate *)birthdate; @property (nonatomic, readonly) NSString *salutation; @property (nonatomic, readonly) NSString *firstName; @property (nonatomic, readonly) NSString *lastName; @property (nonatomic, readonly) NSDate *birthdate; @end
  13. 13. PersonViewController.m - (void)viewDidLoad { [super viewDidLoad]; if (self.model.salutation.length > 0) { self.nameLabel.text = [NSString stringWithFormat:@"%@ %@ %@", self.model.salutation, self.model.firstName, self.model.lastName]; } else { self.nameLabel.text = [NSString stringWithFormat:@"%@ %@", self.model.firstName, self.model.lastName]; } NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEEE MMMM d, yyyy"]; self.birthdateLabel.text = [dateFormatter stringFromDate:model.birthdate]; }
  14. 14. Now with MVVM, PersonViewModel.h @interface PersonViewModel : NSObject - (instancetype)initWithPerson:(Person *)person; @property (nonatomic, readonly) Person *person; @property (nonatomic, readonly) NSString *nameText; @property (nonatomic, readonly) NSString *birthdateText; @end
  15. 15. PersonViewModel.m @implementation PersonViewModel - (instancetype)initWithPerson:(Person *)person { self = [super init]; if (!self) return nil; _person = person; if (person.salutation.length > 0) { _nameText = [NSString stringWithFormat:@"%@ %@ %@", self.person.salutation, self.person.firstName, self.person.lastName]; } else { _nameText = [NSString stringWithFormat:@"%@ %@", self.person.firstName, self.person.lastName]; }
  16. 16. An our view controller - (void)viewDidLoad { [super viewDidLoad]; self.nameLabel.text = self.viewModel.nameText; self.birthdateLabel.text = self.viewModel.birthdateText; }
  17. 17. MVVM Chiva!
  18. 18. MVP Model View Presenter Presenter is a layer that provides View with data from Model. Presenter also handles background tasks.
  19. 19. Why? ◦ Keep it stupid simple ◦ Removes complexity ◦ Easy to test
  20. 20. Normal View Model in Android
  21. 21. On top of that ◦ View can disappear and appear at random time ◦ Do not forget about saving/restoring of Views ◦ Attach a couple of background tasks to that temporary Views
  22. 22. Model View Presenter
  23. 23. MVP Pros ◦ Complex tasks are split into simpler tasks and are easier to solve. ◦ Smaller objects, less bugs, easier to debug. ◦ Testable.
  24. 24. MVP Background Tasks Background Tasks
  25. 25. Code time

×