user1204481
user1204481

Reputation: 177

Applying MVC philosophy in Objective C

I'm starting a small project that displays circles having random radii, random color and random position on the screen. I want to implement this using the MVC paradigm in Objective C.

I have a class Circle that contains the following instance variables:

This class doesn't contain methods, it just holds data. It is put in a separate file. (Circle.m & Circle.h)

I have a myModel class that is supposed to be the model for my MVC. It contains methods that randomly generate centers inside bound of my view, where the bound dimensions are requested from the View throughout the controller.

Every time a random property (that is center, color and radius) is generated, an instance of the Circle class is created within the myModel class, and stored in an NSMutableArray. When the generation is done, this NSMutableArray is passed to the controller, which in turn passes it to the view, thus displaying the circles.

My question is that if I am to implement the MVC paradigm correctly, should :

  1. The Model (myModel) hold instances of Circle, or the instances of Circle should be held by the controller?
  2. My model be made of 1 class, or is it legal to be made of several classes?
  3. The model know the bound size of the view or is that something that a violation in the MVC philosophy?

One last question. If I have made the implementation as I have stated above, are myModel and Circle separate models or both classes constitute one model?

Thank you!

Upvotes: 1

Views: 221

Answers (2)

Caleb
Caleb

Reputation: 125007

[Should] The Model (myModel) hold instances of Circle, or the instances of Circle should be held by the controller?

The model should hold the data. That's it's job. Imagine what would happen if you wanted to change the interface to your program. Instead of (or in addition to) drawing circles on the screen, you might want to display a list of circles and their locations. You'd might want to change or replace the view controller to do that, but you wouldn't need to change the model that stores the circles. Likewise, you might want to change the way that circles are generated, but keep displaying them the way you are now. In that case, you'd change the model, but the view controller and view could probably stay the same.

[Should] My model be made of 1 class, or is it legal to be made of several classes?

A data model is typically a whole graph of objects, very often of different types. You might have one object that manages the rest (although you don't have to). For example, your MyModel class contains an array that stores Circle objects. You could add Square objects, Group objects, etc.

[Should] The model know the bound size of the view or is that something that a violation in the MVC philosophy?

The model shouldn't know specifically about the view, but it's fine for the view controller to tell it to produce circles within a given range of coordinates. That way, if the view changes size or orientation, the view controller will likely know about it, and it can in turn give the model new info.

Upvotes: 2

Conrad Shultz
Conrad Shultz

Reputation: 8808

  1. If you have other components to your model than just circles, wrap everything in myModel. Even if you don't, you might still want to do so to allow for future additions.
  2. Depends on your design. If you are writing a "document based" application (regardless of whether you are using UIDocument) you normally would have a single class that contains the others. Even if you aren't, having a single root class for archiving purposes, etc., is usually convenient.
  3. The model should definitely not know anything about the view hierarchy. (Note that this is different from knowing something like "canvas size" - it would be legitimate to store such a property in the model, and let the view display the canvas however it wishes, such as in a UIScrollView.)

Btw, kudos for thinking about this ahead of time!

Upvotes: 1

Related Questions