Design patterns make your app's code more modular and forgiving when it comes to bug fixes and changes. In this article, you'll be learning about the MVC (Model-View-Controller) and the MVVM (Model-View-ViewModel) design patterns.
Although design patterns (also known as architectural patterns) are key for the development of scalable Cocoa Touch apps, there is a lot of controversy around which architectural pattern is actually best for use in your app.
Check out these recent posts by Bart Jacobs on MVC and MVVM for more perspectives on these design patterns and the choice between them.
Model-View-Controller (MVC) is a widespread software development pattern. Learn what MVC is and why it may not be the best solution for Cocoa developers.
Learn about an alternative to the MVC pattern: Model-View-ViewModel. I'll show you how MVVM can resolve some of the shortcomings of Model-View-Controller.
The collection of objects of a certain [architectural pattern] in an application is sometimes referred to as a layer—for example, model layer. — Apple
Without further ado, let's take a look at a few of the design patterns that you can use in your next app.
This is the most commonly used pattern in Cocoa Touch development, and it's also my personal favorite. This design pattern efficiently and effectively sorts out your code into three categories (or layers): the Model, the View, and the Controller.
With this design pattern, you can make changes to one layer without affecting the other layers of the pattern. For example, if you need to change the database, you'd just remove the model and replace it without having to edit the view or controller. Or if you want to change the way a view looks, you don't have to worry about messing up the database code. This is called "separation of concerns", and it makes your code easier to debug, maintain, and reuse.
In addition, this is the design pattern that is recommended by Apple themselves, and it's a norm in the iOS development community. If you're just starting out, I highly recommend just sticking to this pattern. You can try out different design patterns later in your Cocoa development career.
Let's take a closer look at the different layers in this pattern and what each one is responsible for. Here's a quick diagram of the interactions between the layers:
The MVC design pattern separates each part of your code into one of three parts: the Model, the View, and the Controller.
One thing to keep in mind is that you shouldn't cram too many responsibilities into any one of these layers because that would defeat the purpose of having a design pattern!
Also, if you don't keep the connections between the layers clean and clear, you'll end up with a messy and unmaintainable app, despite using the MVC design pattern! In particular, make sure you don't let the view and the model communicate directly. These interactions must happen solely through the Controller.
Although the Model-View-Controller design pattern is pretty common and should work for most cases, it comes with its own set of challenges and drawbacks. Because of this, we have another design pattern called MVVM, which stands for Model-View-ViewModel.
Well, there's one major problem which you should be aware of. As you saw in the previous section, the Controller layer is the only layer between the View and the Model, so it's not surprising that people abuse this layer and quickly give it too many things to do. This might seem like the easiest thing to do at the time, because it avoids changing the other layers, but eventually it leads to a bloated Controller and difficult-to-maintain code.
This has led to MVC being given the whimsical nickname "Massive-View-Controller" in some circles.
The MVVM architectural pattern, which was borrowed by Cocoa developers from Microsoft, can help combat this problem of massive view controllers. Though it isn't as common in iOS development as MVC, it's increasingly being used to make up for the shortcomings of MVC.
Let's take a look at the different layers and their responsibilities in MVVM. (You should probably note that in the Cocoa community, there aren't any formal guidelines on how to use these layers.)
Here's a quick diagram demonstrating the layers of this architectural pattern and their connections to one another.
If you think about it, you'll see that even though views and controllers are separate layers in the MVC design pattern, they are very closely coupled. So in MVVM, we simply take the View and the Controller and combine them into one layer.
Let's compare each layer to its counterpart in the MVC pattern.
As you've seen previously, you shouldn't mix the responsibilities of any of these layers as this can cause your app code to spiral in complexity, making the use of any design pattern redundant.
I hope you enjoyed learning more about these foundational design patterns for iOS. Hopefully, you gained a better understanding of the MVC and MVVM design patterns and are confident enough to use these in your future applications.
Of course, the architectural pattern which you use for your app is totally up to you, and it depends on the type of application that you're trying to develop. However, if you're new to iOS development, I still highly recommend sticking to the MVC design pattern because it is still the most mainstream in Cocoa development.
While you're still here, be sure to check out some of our other tutorials and articles here on Envato Tuts+!