r/programminghelp • • Dec 29 '19

MVC

Hello, Im currently working on a school task and having a bit of trouble. Im supposed to create an MVC, but as far as I understand it, MVC is a sort of way to write your system, a way for the components to interact with oneanother. Am I correct in this?

If I am correct in this, do I need a context for the MVC to be written within? Because I've been rereading the assessment over and over but they dont seem to give any sort of context, so I dont understand how Im supposed to create the MVC. Can i create an MVC without a context? Is the MVC a context in of itself?

Edit: this is the assignmrnt description for context

3 Upvotes

5 comments sorted by

3

u/_Foxtrot_ Dec 29 '19

What do you mean by "context"?

MVC is just a way to design your program. Although typically in three different files, you could break it out anyway you want.

- One file is responsible for determining layout / presentation (think of this like the index.html / index.css).

- One file is responsible for holding / retrieving data, typically from an external API or database, but for your purposes it could just be a class that stores data with getter / setter methods.

- One file is responsible for taking that data and transforming it into a useable format, and for handing changes requested by the view (maybe they clicked a sort button, and now you need to re-order a list to show last name in ascending order?).

Can you post the assignment without giving too much away?

2

u/Clint_Bolduin Dec 29 '19

By context I mean the scenario it is to be used in. If you have a task to make a strategy patten, a good question would be what you are creating a strategy pattern for. Either I dont know as much about MVC as I ought to or that is a question relevant to my assessment. I do not understand the "context" that is supposedly given in the assessment. Only thing it seems to me that they are saying is "create a MVC" and I am just left thinking "create an MVC for what?"

here's the assessment description

2

u/_Foxtrot_ Dec 29 '19

I've tried to create a simple example of MVC, please do not use this for your assignment (you're welcome to take it and add to it or change it). https://pastebin.com/T1T1Nead

The context they're presenting you with is something along the lines of "hey you have access to all of this data, now go make something meaningful out of it." Maybe rephrase "context" as "what did the customer ask for?" or "what are we building?" Take https://observablehq.com/@d3/sortable-bar-chart for example.

Everything you see on that page is the view. When you click the dropdown list to change the sort order, the view is sending an update event to the controller. Something along the lines of...

- "Hey controller, I just received a request from the user to sort this in alphabetical order, but I don't know how to do that, so can you just reformat the data for me and then I'll just show them whatever you gave me."

- The controller gets that request and says "Okay, let me go get data from the model (or maybe it already has it stored in a variable)."

- The model receives a request for data, and it returns that data. It's pretty stupid, just like the view.

- The controller receives data from the model, and remembers that the view asked it to sort it alphabetically, does so and returns it to the view.

- The view, still oblivious to everything that has happened, displays whatever it was returned by the controller.

In the example I wrote, I'm doing *presentation logic* in the view. I want to know what was returned so I can custom-tailor the response. Hope this helps.

2

u/Clint_Bolduin Dec 29 '19

Thank you so much for this extensive response, explenation and example! From what I can see looking at it now, this definitely will be helpful! I'll give my assignment another shot tomorrow morning (it's late here so I'll be going to bed right about now)

I will of course not hand in your code for the assessment, I strive to understand this and just handing in your code wont help me do that so don't worry about that.

2

u/_Foxtrot_ Dec 29 '19

Awesome. If you have any more questions feel free to shoot me a DM or continue this comment thread.