Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

iOS App Design Patterns – One Interesting Pattern That Can Help

DZone's Guide to

iOS App Design Patterns – One Interesting Pattern That Can Help

See how the MVP design pattern works for iOS applications developed with Swift, and a code sample showing the pattern in action.

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Design patterns make programmers jobs just that little bit easier. And if you’re a programmer you’ll know that the easiest way to get your job done is always the best way. Design patterns are templates for creating software using a repeatable process. These templates are applied to a common problem to help reduce the risk of contraindicative bugs occurring and to separate out the core elements of the application, along with each of their interactions, making unit testing more effective.

Design patterns are best used with Object Oriented (OO) programming languages. Since Swift is the language of iOS, this means that we can apply applicable design patterns when we are developing our iOS apps.

The Model-View-Presenter Design Pattern

One of the most useful design patterns that we can apply to iOS apps is the Model-View-Presenter (MVP) pattern. This pattern allows for effective sectioning of an app into its UI, the data access, and the logic behind it all.

The Model

Within this pattern, our data access is the Model part of the puzzle. This is the interface to our database, server, or other data store, and what we use to populate parts of our UI.

The View

Our UI element is the View part of the pattern. Here we change the UI from incoming data that is received, as well as accept user interactions, that are passed to a handler.

The Presenter

The Presenter is our logic within the app, which passes along changes to both the UI (the View) as well as the Model. It handles any complex logic, you can think of the View and the Model as mainly dumb interfaces.

presenter

The MVP Pattern in Action

Now that we’ve learned the basics of the MVP design pattern and why it is useful, let’s take a look at the MVP pattern in action with a Swift basic implementation. This implementation will involve building the MVP components of a very basic app that lets a user rate movies from the UI interface.

First, we should set up our View protocol, MovieView, which will publish our movie name and movie rating to our UI. These will need to be implemented from within the actual View class:

func setMovieName(movieName: String);
 func setMovieRating(movieRating: String);

Then we will show the MovieViewController class which will be the actual View for our app. It inherits from the UIViewController as well as our MovieView protocol. We will need to create an instance of our Presenter from within this class:

var presenter: MovieViewPresenter!
 let movieName = UITextField()
 let movieRating = UITextField()
 let findMovieButton = UIButton()
 findMovieButton =1
 let saveMovieRatingButton = UIButton()
 saveMovieRatingButton =2

If our view loaded correctly, then we’ll have to add a listener to MovieViewController for our buttons to check what the user is doing, which we can then pass to the Presenter:

 override func viewDidLoad() {
 super.viewDidLoad()
 self. findMovieButton.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside)
 self. saveMovieRatingButton.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside)
 }

func didTapButton(button: UIButton) {
 switch(button.tag)
 {
 case 1:
 self.presenter.findMovie(movieName.text)
 break
 case 2:
 self.presenter.saveMovieRating(movieName.text, movieRating.text)
 break
 }
 }

These two methods would then be stipulated in our protocol to the Presenter class, MovieViewPresenter which should also stipulate an incoming View, for instantiation:

init(view: MovieView, person: Person)
 func findMovie(movieName)
 func saveMovieRating(movieName, movieRating)

And then defined in our actual Presenter, MoviePresenter:

unowned let view: MovieView
 let model: MovieModel
 required init(view: MovieView, model: MovieModel) {
 self.view = view
 self.model = model
 }
 func findMovie(movieName) {
 let movieRating = self.model.getMovieRating(movieName)
 self.view.setMovieRating(movieRating)
 }
 func saveMovieRating(movieName, movieRating) {
 self.model.saveMovieRating(movieName, movieRating)
 }
 The model itself is trivial to implement, but this is how we then hook it all up in the end:
 let model = Model()
 let view = MovieViewController()
 let presenter =MoviePresenter(view: view, model: model)
 view.presenter = presenter

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
mobile ,mvp ,mobile app development ,ios ,swift ,object oriented programming

Published at DZone with permission of Sitanshu Joshi. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}