5

I've been told that MVC is an architecture pattern for the presentation tier on a multi-tier architecture. I didn't understand the MVC Model concept.

So I made a web application, as an aproach to learn MVC model, only using JSP and Servlets, I made the DataAccess layer too.

In my WebContent folder, I put all the views.

In my src (Java content) folder, I made 4 packages.

data presentation.model presentation.controller businessEntities 

I used Servlets as Controllers (used the FrontController pattern) and JSP for all the Views.

As the title says, my problem is with the model.

Now, lets make an example..

I have 2 classes.

 - Car - CarLogic 

CarLogic is a class that calls a possible CarDAO to get data and to return it to anyone who asks it. Car is just the POJO.

I placed CarLogic inside presentation.model and Car on businessEntities.

Am i wrong? What should i put on Model package?

Thanks!

6 Answers 6

3

When nested in the presentation layer of a multi-tier architecture, MVC's models are usually nothing more than (possibly enhanced) key-value maps.

The controller, after having called the appropriate business layer service, instanciate a new map with the values the view will have to display.

Spring MVC for example implemented the MVC pattern this way: see how the ModelMap extends LinkedHashMap.

See this answer to see a typical HTTP request life cycle, when the MVC pattern is nested in the presentation layer of a 3-tier architecture.

So to sum up, I would put CarLogic in your businessEntities package, and the Car POJO within a new presentation.bean package for example (so you won't need your presentation.model package anymore). Thus, your controller would add the Car instance as an attribute of the request, which would then play the part of the model:

request.setAttribute("theBean", yourBeanInstance); 

In your JSP views, simply use ${theBean.anyProperty} to access the attribute (don't forget the getter in your bean). Note that the EL will scan the following scopes in given order: page, request, session, application; the first found match will then be returned.

Sign up to request clarification or add additional context in comments.

6 Comments

In this case, is the View a Java Class in my project? Do you have any example that applies this without using SpringMVC? / Just saw your edit. In what cases should I need model classes?
@pablo.raissiguier What do you mean by model classes? Your Car POJO for example?
If Car is a business entity, why should it go to presentation layer?!
That's just what i did! Setting up attributes with my bean instance and used the RequestDispatcher to forward the response and request to the view (a JSP page). To answer your question, I was talking about making the key-value maps that you mentioned before. I think the same as Adam.
@AdamDyga I usually try to use presentation-specific beans, so that if the presentation need changes, you don't have to update the other layers (the business one in this case). Well, in small projects, it does not really matter actually.
|
3

Model View Controller in and of itself is a pretty simple pattern.

From the wikipedia artical:

  • A controller can send commands to the model to update the model's state (e.g., editing a document). It can also send commands to its associated view to change the view's presentation of the model (e.g., by scrolling through a document).
  • A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands. In some cases an MVC implementation might instead be "passive," so that other components must poll the model for updates rather than being notified.
  • A view requests information from the model that it needs for generating an output representation to the user

At least it was simple pre-web and pre-SOA.

The view part is still pretty straight forward, it simply presents the model.

Controller-wise, the web, in its traditional http/html sense rather than the newer JavaScript/AJAX/websocket sense, has made the controllers iteraction with the view much more visible than it was in say a Swing application. This means in addition to their traditional role, controllers in a web-app are likely to not only interact with the model but also have some knowledge of the views.

Model-wise, pre-SOA the model represented both the data and the behaviour of the business. More recently anemic domain models have become popular with behaviour implemented in separate classes from the representation of the data, which is passed to the logic classes (services).

It's this point, relating to the separation of data and logic that I believe your question relates to. Personally, for simple systems, I prefer a rich model with the data and behaviour in the same classes, hence the packaging is simple. However I appreciate that sometimes the benefits in separating the logic from the data and in such cases I'd create a separate package (and probably a separate jar) to contain that logic.

2 Comments

This almost but not quite gets to what seems, to me, like the point of the OP's uncertainty. That is, that the domain model is not the whole of the model (in the MVC sense of the term), especially when you're using an anemic domain model. Whileas, in the cookie-cutter tiered application structure, you would have a "model layer", and a "business logic layer" – where "model" means just the domain model alone, i.e. not exactly what the word means in "model-view-controller".
@millimoose agreed, in my answer read 'service' as 'business logic', whether it's in the same JVM or another. Either way it does corrupt the pure model in the strict MVC sense.
3

The word "model" means subtly different things in the term "model-view-controller", and when you say "model layer" in an n-tier architecture such as yours. From the point of view of MVC, everything the controller passes input to, and fetches viewable data from, is the "model".

This includes your domain model (Car), and business logic (CarLogic). You don't need to necessarily structure your application to exactly match the pattern you're using on the UI side. (In fact, the MVC pattern is probably insufficient to cover an entire application to begin with.)

So in your case, putting Car into businessEntities is fine. Not sure about having presentation.model to begin with – business logic isn't a presentation concern, and probably belongs in a layer on its own. Which is where I'd put CarLogic.

I have no idea what the data package is supposed to be at all.

This split means there doesn't seem to be a dedicated presentation model in your application. This might or might not be an issue. In a web application, what the presentation model usually does is encapsulate user input, or maps the business entities to a structure that fits the needs of the view better – flattening complex relationships etc. (In classical Spring MVC, these would be the Command and Model classes.) In a simple enough application, or using an advanced enough web framework, it's possible to directly use domain model entities in your views.

As an example of what a distinct presentation model would do, consider the internal system a school would use. You'd have a bunch of students, and some classes they can enroll in. At the business entity layer, you'd probably have a separate Enrollment entity that would hold attributes like date of enrollment, references to the grades a student got in the class, etc. However, when a view in the application needs to display the list of classes a student is enrolled in, it doesn't really need to see the Enrollment object at all. Thus it makes sense to have a presentation model object Student that only has a list of Class objects in it. (And the requisite code that reads the business entities and maps them correctly to this structure.)

1 Comment

data package contains all the DAO classes and their helpers that retrieves data from a MySQL DataBase. BusinessLogic layer uses Data layer to bring data to the Controller. Just using a pseudo-Indirection pattern
2

From Wikipedia about MVC:

Model–view–controller (MVC) is a software pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user. The central component, the model, consists of application data, business rules, logic, and functions. A view can be any output representation of information, such as a chart or a diagram. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants. The third part, the controller, accepts input and converts it to commands for the model or view.

I highlighted one sentence, that usually is not understood. The model is a really rich model. That means, it also contains every logic that works with the data. It is up to you, whether to have separate classes for data and for logic (which reduces the data classes to structs or records), or whether to have richer data classes that already contain some business methods (the way that I prefer).

That said, the controller is just the interaction between the model and the view. In Swing applications - for example - the controller is nothing else than the various listeners (an ActionListener, for example).

So, to answer your question: Having a separate data access layer is a good thing. This is where your DAO class belongs to. The data and logic classes (the model) belongs to the model package. I would not create several packages for them.

2 Comments

Yes, I know. But if you think about it, the presentation tier should only manage all the things related to display the data in the view. On Swing applications, presentation tier only has Swing forms.
There are two paradigms when talking about the MVC pattern: either 1) as an architecture for a whole application, or 2) nested in the presentation layer of a n-tier architecture. You're explaining the 1) while the OP is trying to use the 2).
1

MVC is a common design pattern used by many modern web frameworks, f.e. GRAILS, Spring MVC or ASP.NET MVC. You see the MVC pattern is included in their names ;)

For the MVC-pattern you actually need three packages:

  • model
  • controller
  • view

In the model package you put your classes which will be used to hold your data. The controller classes contains the logic to controll which view is called and which data should be used in those views. The views itselfs contains the information how to display your data on the screen. There shouldn't be any great logic, f.e. just JavaScript. The business logic should contained in a separate package. If you need this logic, you can call is from your controller.

Your packages are (I hope i read them correctly!):

  • data
  • presentation.model
  • presentation.controller
  • businessEntities

According to the MVC-pattern is suggest the following usage of your packages:

  • "data" or "model" - use this for your Car class
  • "controller" - use this for your controller logic
  • "presentation" or "views" - use this for your views to display your data on the screen
  • "businessLogic" - use this for your CarLogic class

Comments

0

MVC stands for Model View Controller.

Model is where you store entity objects View you store Presentation objects Controller is where you map your attributes that are present in the Model to the widgets.

so that the attributes are set to the entity objects from the widgets input.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.