Model-View-Controller (MVC) Explained with Food, Because Food is Delicious

Well obviously asking your bread basket to replicate won’t accomplish much.

You would need to ask the server for more bread which would initiate a new process.

It is important to have a well organized restaurant.

In theory the faster the server delivers dishes, the more money they will make.

You want to minimize the amount of time it takes to search and deliver.

This is also why all the curries are prepared ahead of time.

Multiple Developers can work on the MVC concurrently.

MVC can be used for most web development languagesI will however, be demonstrating the example with Java.

Curry is your model class.

It includes all the private ingredients with your getters and setters.

public class Curry{ private String name; private Boolean includeYogurt; private String bread; public String getName() { return name; } public void setName(String name) { this.

name = name; } public Boolean getIncludeYogurt() { return includeYogurt; } public void setIncludeYogurt(Boolean includeYogurt) { this.

includeYogurt = includeYogurt; } public String getBread() { return bread; } public void setBread(String bread) { this.

bread = bread; }}Here is the brains of the meal preparation, your controller.

public class ServerController{ private Curry model; private CurryView view; public ServerController(Curry model, CurryView view) { this.

model = model; this.

view = view; } public void setCurryName(String name) { model.

setName(name); } public String getCurryName() { return model.

getName(); } public void setCurryBread(String bread) { model.

setBread(bread); } public String getCurryBread() { return model.

getBread(); } public void setCurryIncludeYogurt(Boolean includeYogurt) { model.

setIncludeYogurt(includeYogurt); } public Boolean getCurryIncludeYogurt() { return model.

getIncludeYogurt(); } public void updateView() { view.

printCurryDetails(model.

getName(), model.

getBread(), model.

getIncludeYogurt()); }}Here is the view or what you would see output to the screen.

public class CurryView{ //what you can see on your table public void printCurryDetails(String curryName, String bread, Boolean includeYogurt) { System.

out.

println("Curry: "); System.

out.

println("Name: " + curryName); System.

out.

println("Bread Choice: " + bread); System.

out.

println("Yogurt: " + includeYogurt); }}And finally, here you are as the user.

This code demonstrates what would happen if you decided to order another meal after your first.

Try running the code in your IDE to understand the flow better.

public class User { public static void main(String[] args) { Curry model = retriveCurryFromDatabase(); CurryView view = new CurryView(); ServerController controller = new ServerController(model, view); controller.

updateView(); //You ordering another meal controller.

setCurryName("Chicken Tikka"); controller.

updateView(); } private static Curry retriveCurryFromDatabase() { Curry curry = new Curry(); curry.

setName("Bhindi"); curry.

setBread("Paratha"); curry.

setIncludeYogurt(true); return curry; }}Note: I realize this is not the best example to code, but in following with a good analogy I like to follow up with application.

If you want more examples try a TODO app or this Student example from tutorialspoint.

.

. More details

Leave a Reply