I have read a lot about MVC design pattern, but some of the things are still unclear to me. I know that "Model" is for data and business logic, "View" is for presentation and "Controller" is for using "Models" and providing "Views" (i.e. C is the communication channel between M and V).
Now, I have the following problem I want to solve:
Problem: The web application takes as input, a list of Nodes from a user. Then, a Model is used to make a Graph (i.e. Data Structure Graph and not x-y graph) out of those nodes (using a database).
I then use Dijkstra's algorithm to find out the shortest path from a starting node to an ending node in that graph. Do I use the Dijkstra's algorithm in the Model or the Controller?
I think I should use the Model layer because the "shortest path" itself is data.
But sometimes, I think I should put it in the Controller because it uses the Models (Graph and List of nodes) to do something.
Can anyone give me the right answer? For now I am going to implement Dijkstra's algorithm in the model layer.
Yes you are right. You should put your Dijkstra's algorithm in model. The reason is that you may use a different algorithm to find shortest path tomorrow, so in that case you need not change your controller, just change the logic of class that implements the algorithm. And the outcome of the this algorithm should be incorporated in view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With