0

i struggle understanding the concept of Model View Controller.

As far as I know the purpose of this model is to divide the whole System in to 3 PARTS. One part is responsible for the data and behavior and data, one for the UI and one for the interaction between those two.

But here is my problem: How do I get from the Concept to the Implementation for a big Project?

My Teacher had this UML Class Model where there are only 3 Classes, but how can the behavior of the whole System be ONE Class?

Is the model Class more like a Interface that all models have to implement?

Why this Question is different: I get how the Model works when you have one class that does all your stuff (like a calculator) but what if I need more than one class for a more complex System?

Hope you guys get my question

  • 1
    I am not sure we do buddy, especially this sentence: `My Teacher had this UML Class Model where there are only 3 Classes, but how can the behavior of the whole System be ONE Class?`, can you elaborate more on it? – Akheloes Jun 10 '18 at 08:03

2 Answers2

0

MVC is one of Compound Patterns, i.e. it consists of three other design patterns:

  1. Strategy - here, Controller provides methods to handle user input from View. View delegates operations made by user to Controller, which knows what to do.

  2. Observer - Controller, as well as the View need to know when the Model has changed, thus View is observable and two other are observers. Slight modification could be to define one observer - Controller and make it update the View accordingly to the change.

  3. Composite - it is a way to represent GUI.

Learn about these three patterns and then you'll have full understanding how to implement more complex system.

Also, you could google phrase: MVC as compund pattern.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • Actually I like to think of MVC as a specific instance of the mediator pattern: the Controller mediates between the View and the Model – Dexygen Jun 10 '18 at 11:00
0

The model represents the part of the application that implements the logic, manages the data and defines the behavior of the application. The model responds to:

  • the view information requests about its state updating it
  • instructions to change state from the controller.

The View reproduces the contents of the model. It specifies exactly how the model data should be presented. Whenever the model data changes, the view must update its representation as needed.

The controller is responsible for handling the user requests. It is the link between the client and the system and ensures that all the resources needed for completing a task are delegated correctly.

Once the controller knows which action needs to be performed it delegates the rendering process to the View layer.

In other words, the user uses the controller. The controller manipulates the model. The model updates the view that the users sees after a specific request.

Regarding the implementation, you can have one class for the controller that holds a reference to the model manager class and invokes the methods on that object reference depending on the user requests. For example:

    case "List":
           view.show("" + model.getAll());
           break;
    case "Add":
           String input = view.get("title");
           String input1 = view.get("artist");
           String track1 = view.get("Track title");
           String track2 = view.get("Track artist");

           if (input == null) return;
           String msg= "";
           view.show(msg);
           CdTrack  cdTrack = new CdTrack(track1, track2, new Time(0,0,0));
           CdTrack[] cdsTrack = {cdTrack};
           model.addCd(new Cd(input,input1,cdsTrack));
           view.show(msg);
           break;

The view class should pass the request to the controller. For example:

@Override
   public void actionPerformed(ActionEvent e)
   {
       if(((JButton) e.getSource()).getText().startsWith("List"))
           controller.execute("List");
       if(((JButton) e.getSource()).getText().startsWith("Add"))
           controller.execute("Add");
   }
  • But what do you mean by "represents the part". Is model ONE class or an interface or a module of multiple classes? – Lorenzo Siemens Jun 10 '18 at 14:40
  • With "represents the part" I basically mean "is the part" (The model implements the logic of the application). Normally you would have one class implementing an interface and that interface contains all the methods of the application. For example, if you want to implement the CRUD operations in the model class, those methods would be declared in an interface that the model class would later implement. –  Jun 10 '18 at 15:05