model-view-controller: tips&tricks

37
Design Pattern MVC July 17th, 2014

Upload: ciklum

Post on 15-Jan-2015

92 views

Category:

Technology


2 download

DESCRIPTION

The 2nd Dnepropetrovsk iOS Practice Leaders Community Meet-Up. MVC by Dmitriy Sai, iOS developer with 3+ years of experience. The following presentation covers the issues of the practical use of Model View Controller on iOS, focusing on the principles of roles’ division, clear code, simplification of refactoring and unit-testing.

TRANSCRIPT

Page 1: Model-View-Controller: Tips&Tricks

Design Pattern

MVC July 17th, 2014

Page 2: Model-View-Controller: Tips&Tricks

MVC

Page 3: Model-View-Controller: Tips&Tricks

Agenda

● History ● MVC● Controller specifications● View specifications● Model specifications● Demo

Page 4: Model-View-Controller: Tips&Tricks

History

Model View Controller (MVC) is one of the most quoted (and most misquoted) patterns around. It started as a framework developed by Trygve Reenskaug for the Smalltalk platform in the late 1970s. Since then it has played an influential role in most UI frameworks and in the thinking about UI design.

Page 5: Model-View-Controller: Tips&Tricks

MVC● Controller sends commands to the model to update state.

Also sends commands to its associated view to change the appearance.

Page 6: Model-View-Controller: Tips&Tricks

MVC● Controller sends commands to the model to update state.

Also sends commands to its associated view to change the appearance.

● A model notifies its controllers when there has been a change in its state. This notification allows the views to produce updated output.

Page 7: Model-View-Controller: Tips&Tricks

MVC● Controller sends commands to the model to update state.

Also sends commands to its associated view to change the appearance.

● A model notifies its controllers when there has been a change in its state. This notification allows the views to produce updated output.

● A view requests information from the model that it needs for generating an output representation to the user.

Page 8: Model-View-Controller: Tips&Tricks

MVCModel specifications:

● business logic

Page 9: Model-View-Controller: Tips&Tricks

MVCModel specifications:

● business logic ● has knowledge about itself but does not know

about the controllers and views;

Page 10: Model-View-Controller: Tips&Tricks

MVCModel specifications:

● business logic ● has knowledge about itself but does not know

about the controllers and views;● in some cases is a database manager or a set

of objects;

Page 11: Model-View-Controller: Tips&Tricks

MVC Business logic - real-world business rules that determine how data can be created, displayed, stored, and changed.

Page 12: Model-View-Controller: Tips&Tricks

Model

Model base types:- passive model- active model (basic type)

Page 13: Model-View-Controller: Tips&Tricks

MVC

Model

ApiClient DataManager

update UI

Page 14: Model-View-Controller: Tips&Tricks

MVC@property (readonly) DPManagerProvider *provider;

Page 15: Model-View-Controller: Tips&Tricks

MVC@property (readonly) DPManagerProvider *provider;

#dynamic state

@property (readonly) NSString *email;

@property (readonly) NSString *password;

@property (readonly) NSString *messageText;

@property (readonly) BOOL canEdit;

Page 16: Model-View-Controller: Tips&Tricks

MVC@property (readonly) DPManagerProvider *provider;

#dynamic state

@property (readonly) NSString *email;

@property (readonly) NSString *password;

@property (readonly) NSString *messageText;

@property (readonly) BOOL canEdit;

#static states@property (readonly) NSString *emailDefaultText;

@property (readonly) NSString *passwordDefaultText;

@property (readonly) NSString *title;

@property (readonly) NSString *loginTitle;

Page 17: Model-View-Controller: Tips&Tricks

MVC@property (readonly) DPManagerProvider *provider;

#dynamic state

@property (readonly) NSString *email;

@property (readonly) NSString *password;

@property (readonly) NSString *messageText;

@property (readonly) BOOL canEdit;

#static states@property (readonly) NSString *emailDefaultText;

@property (readonly) NSString *passwordDefaultText;

@property (readonly) NSString *title;

@property (readonly) NSString *loginTitle;

#data access methods

- (void)login;

- (BOOL)shouldChangeEmailInRange:(NSRange)range

replacementText:(NSString *)text;

- (BOOL)shouldChangePasswordtInRange:(NSRange)range

replacementText:(NSString *)text;

Page 18: Model-View-Controller: Tips&Tricks

MVCControllerModel

Page 19: Model-View-Controller: Tips&Tricks

MVCControllerModel

● Notification

Page 20: Model-View-Controller: Tips&Tricks

MVCControllerModel

● Notification● KVO

Page 21: Model-View-Controller: Tips&Tricks

MVCControllerModel

● Notification● KVO● Delegate

Page 22: Model-View-Controller: Tips&Tricks

MVCControllerModel

● Notification● KVO● Delegate● Binding

Page 23: Model-View-Controller: Tips&Tricks

MVCController specifications:

● can transmit data

Page 24: Model-View-Controller: Tips&Tricks

MVCController specifications:

● can transmit data ● can place Views on the screen

Page 25: Model-View-Controller: Tips&Tricks

MVCController specifications:

● can transmit data ● can place Views on the screen ● can respond to user’s actions

Page 26: Model-View-Controller: Tips&Tricks

MVCController specifications:

● can transmit data ● can place Views on the screen ● can respond to user’s actions ● can operate with several Views

Page 27: Model-View-Controller: Tips&Tricks

MVCView specifications:

● can display data

Page 28: Model-View-Controller: Tips&Tricks

MVCView specifications:

● can display data ● can, in some cases, have code that

implements some business logic

Page 29: Model-View-Controller: Tips&Tricks

MVC

Controller

Model View

@ view;@ model;

Page 30: Model-View-Controller: Tips&Tricks

MVC

Controller

Model View

View

Page 31: Model-View-Controller: Tips&Tricks

MVC

Controller

Model

ViewController

View

Page 32: Model-View-Controller: Tips&Tricks

MVC

Controller

Model View

View

Controller

Page 33: Model-View-Controller: Tips&Tricks

MVC

Controller

ModelView

Page 34: Model-View-Controller: Tips&Tricks

MVC

Controller

ModelView

View

Controller

Model@parameters

Page 35: Model-View-Controller: Tips&Tricks

MVC

Controller

Model

Storyboard

Controller

Model @parameters

Page 36: Model-View-Controller: Tips&Tricks

MVCAdditional materials:

● Msdn MVC

● MVC https://developer.apple.com

● Refactoring code

http://martinfowler.com/books/refactoring.html

● Steve McKinley “Perfect Code”

● Eric Freeman “Design Patterns”

Page 37: Model-View-Controller: Tips&Tricks

Demo: dima.sai github - dsay