Learn about the Weather app and how to implement it using POP principles.
- [Instructor] We aim to implement a simple, yet functional weather app, using POP principles. The app will display weather information for a given city by getting the data from a web server. Let's start designing the app. The weather app follows the Model-View-Controller design pattern. This high-level pattern addresses the global architecture of an application. The MVC stands at the core of most modern applications and frameworks, including popular frameworks like The Cocoa Touch.
iOS and macOS apps actually make us follow the rules defined by MVC. So, let's take a closer look at this design pattern. The MVC pattern classifies the objects based on the roles they play in an application. We have three types of objects; the Model objects hold the application's data and they find the logic to manage that data. Model objects should not include presentational logic. View objects present information to the user and may also allow modifying to data from the application's data model.
The view object can cache the data it displays, but it must not be concerned about storing for data. Controller objects act as a mediator between the views and the model objects. They play the role of intermediary between the views and the model objects. A controller object let's the views access the model instances that encapsulates the information they need to display. Also, the controller modifies the views when relevant object changes occur. Now that we saw what the MVC stands for, let's go back to the weather app.
We need to fetch weather data from the internet using a web service. Now I would like to talk about an anti-pattern. Specifically, about polluting our ViewController's with random isolating network calls. This approach may work for smaller projects. Yet, is going to cause issues and maintenance problems when the project grows bigger. The solution is to define a NetworkController. This dedicated controller encapsulates all network and related tasks. It exposes a narrow set of public methods required to gather data out of the network.
Our NetworkController pulls data using a weather app service. The pay-load is usually in one of the popular formats, typically JSON or XML. After decoding the values, we can put the data into our model objects. The views in our app need to access the weather data, which is supplied by the NetworkController. The fact that we don't need to update the model, simplifies the ViewController code to a great extent. So, this is our high-level design.
We identified the key players, now let's start implementing them.
- Comparing object-oriented programming with protocol-oriented programming
- Methods and class-bound protocols
- Adopting a protocol
- Declaring asynchronous behavior
- Preparing and implementing fallback logic
- Implementing an app using protocol-oriented programming