Join Ron Buencamino for an in-depth discussion in this video Understanding model objects, part of Learning the Model-View-Controller Design Pattern in iOS.
- For us to gain a deeper understanding of the relationships between MVC elements, we need to go in depth on each element's specific role. The first MVC element that we're going to discuss is the Model. So how can we define a model more clearly? Simply put, they are the parts of our program that deal with information that we want to process. For example, let's create a Shopping List App. This list is going to be made up of items that we have to shop for.
Now each item on this list has a unique attribute such as the name of the product, the quantity that we need to purchase and the name of the store that we're going to buy the product from. In this application, this could be our Item Model Object. Now if we were to make an App to sell cars, then the list of the cars that are available for sale would be made up of different cars that are offered from our dealership.
So we would have a Car Model Object and it would be made up of such attributes as the make of the car or the model or the year that it was manufactured. Now from these two examples, we can gather the first characteristic of a model object which is that it is a collection of data that we are using within our system. They also represent the knowledge and experience related to a specific problem domain.
It also needs to be reiterated that models are reusable. You should be able to call on a model object at any point in your application and manipulate the data that it represents without affecting other parts of the system that also reference the model data. Another point that needs to be stressed is that your model should not have any explicit connection to your User Interface. As a reminder, if you couple the model to the view, you are not building a moderate MVC application that's for use within Cocoa and Cocoa Touch.
Now when first interacting with data in our application, it is an easy habit to dynamically create our models using standard NSDictionary Objects and run times and keep it contained within the methods we used to retrieve or save data. Now, this may work if we are creating sample code, or if we are just testing something out, however this chunk of code isn't reusable outside of the class that it was created in. In xcode, we typically build our models and CoreData by creating custom entities and each of these entities have their own unique set of attributes which are clearly defined all the way down to the data type each attribute should be.
Now refering back to our Shopping List Application, if we were to build a model for the list itself, we would create an Item entity. Now for the attributes, we would have an attribute for the product name, which would be a String value, and we would have an attribute for the quantity, which would probably be a double. We'll also have another attribute for the Store Name that we would be purchasing the item from, which would also be a string. There are a number of other attributes that could make up the Item entity, but the point here is to show the differentiation between the Model entity itself and the attributes that describe and make up the entity.