I am making an app for manipulating matrices and I’d like to know how to set up a good MVC system for it.
Basically, I’ve got that I’ll need 3 main things:
- MatrixRow (holds the entries of each row and can manipulate the rows by multiplication)
- Matrix (holds a bunch of matrix rows, and can add new rows, subtract rows, change rows)
- ProblemViewController (shows the matrix and allows user to interact with the matrix to solve it)
But I’m confused mainly with when to use a View, or a ViewController. Views seem easier for MatrixRow and Matrix because they’ll just ultimately be subviews added to the ProblemViewController, but is it acceptable to throw in logic into a view?
In terms of models, do I make a Matrix model which has an array that holds all the entries? And a MatrixRow model which holds all the logic to multiply rows? Or is logic not supposed to be in a model.
The whole point of the MVC architecture is to separate data and business logic (model) from presentation (view). You might have view classes that know how to display matrix rows, or entire matrices, but the objects that actually store and manipulate the data should be separate from those.
A model for your matrix could look something like this:
So the Matrix class stores the data, and it provides operations that make sense for a matrix. It doesn’t know how to display the data on screen, or where the data comes from, or how a given matrix might be used in your application.
Exactly how you set up your program is partly a matter of personal preference — some people seem to like very bare models that do nothing but store data. Personally, I’d put everything in the model that’s relevant for a matrix in general and leave out everything that’s specific to how you plan to present matrices within your app.
Imagine that you’ve just finished your app, and your next task is to convert it to provide a completely different kind of user interface, like a command line interface or a web interface. If you were to do that, would you expect to have to rewrite your matrix multiplication code? Would the definition of the matrix inverse change? “No” on both counts — those things belong in the model. Your entire Matrix class should be reusable in a program that does the same thing but provides a different UI.