use of mvc in project - java

i am a student(8th semester I.T.).i want to know the use of MVC(model view controller)in project...please explain..thanks

MVC is an architectural pattern which can be used to organize the logic within an application. Unsurprisingly, it separates the logic into Models (business logic, the bulk of the knowledge in the code), Views (UI display and logic) and Controllers (sort of an eventing model for the UI to interact with the business logic).
This is by no means the only pattern one can use. But it's a commonly used one and fairly simple to understand. Whether or not to use it is an architectural decision on a case-by-case (or project-by-project) basis.

this should get you started:
Java SE Application Design With MVC

MVC(Model | View | Controller) is a compound design pattern made of several design patterns. If you need to learn this pattern easily learn about the Observer pattern and Strategy pattern which MVC is made of. Models and Views use Observer pattern, there views are observers of models. When ever there is a change in the view, view trigger the controller to update the model. Then controller updates the model and then as View is the observer of the model model updates the view.

Related

In MVC pattern, can the Model interact / modify the View?

The MVC pattern component interactions are described this way on Wikipedia:
The model is responsible for managing the data of the application. It
receives user input from the controller. The view means presentation
of the model in a particular format. The controller responds to the
user input and performs interactions on the data model objects. The
controller receives the input, optionally validates it and then passes
the input to the model.
I understand that the View should not be able to interact with the Model. But in most of the diagrams I find on the net, MVC is represented like this:
We can see that Model does interact with the View and is able to modify it, and it doesn't make sense.
Doesn't the Model update the Controller, that updates the View?
What am I missing?
The MVC architecture was created in the 1970s. Obviously there was no Internet at that time. In the original version, the Model directly updates the View through data binding, also known as publish/subscribe, also known as the Observer Pattern.
The Gang of Four Design Patterns book describes this MVC architecture in detail. A couple of quotes from that book are in another answer here.
The MVC architecture was very popular, and when the Internet came along, developers wanted to continue using it; but it didn't fit nicely into client/server applications. Thus was born "WebMVC", the version you most commonly see today. WebMVC is typically implemented as a layered architecture, which the original was not.
Confusion ensues when the two architectures are conflated. Often both are referred to simply as MVC. Even worse, related architectures such as MVP and MVVM can be called MVC.
Personally, I find the relationship between desktop MVC and web MVC somewhat like the relationship between Java and JavaScript. The latter piggybacked on the famous name of the former, to implement something significantly different.
related: Is it MVC when the view doesn't interact with the model?
No you can't access view with model directly, you must access controller first
as its MVC Pattern
Diagrams - worth a thousand words! The precise words and context used in the diagram maybe doesn't tell a story of implementation, say in Microsoft MVC 5/6.
A user's interaction is with the controller. Not the view and not the model. Calling an action on a controller will return something (a view, a file, a redirect, etc.).
In the case of returning a view of information, the controller, having worked out what data the user is requesting can retrieve a model that fits the request, pass this into a view, and return the view result of this model.
In the diagram above it isn't clear that the controller is acting as the agent in moving the model into the view. the model does not decide on the view. Why? Depending on what is in the model returned the controller, a different view might be returned. That is why the controller is aptly named. It sits at the centre of affairs making decisions and moving objects around.
So what you are missing is some context about how the process of MVC occurs when implemented

Explanation of correct using MVC pattern

I have read this tutorial: http://www.tutorialspoint.com/design_pattern/mvc_pattern.htm
I have found it very understandable, but the structure of this Model-View-Controller is different from other examples.
In this tutorial, Controller is using data from Model (Student) and is updating View (StudentView) with new data.
But according the scheme on this picture, Controller is manipulating the Model and Model is updating View. According to this scheme Controller has link to Model, and Model has link to View (or I am wrong?).
This difference is confusing me. Can anyone help me and explain me the correct structure of MVC pattern.
There is no one MVC pattern as each language or framework will define it slightly different. We know Controller encapsulate the business logic, process the user request, and send it to the Model. The Model contains the data and may contains some logic to validate itself or notify subscribers of change. The View formats the data, responds to notifies from the model or controller, and presents it to the user. Typically MVC refers to the patterns Observer, Composite, and Strategy and will often employ Factory, Decorator, and other patterns to achieve MVC. (Design Patterns Elements of Reusable Object-Orient Software)
As to the diagram, I would say it is mostly accurate. Just a poor choice of wording. Update vs Notify

MVC pattern vs Three tier - where does logic belong to?

I am trying to introduce some good practices into my website coding, so I started to look into MVC, since it is a buzzword in website designing :-) I am confused by the MVC pattern though. I am used to thinking in a Three-tier pattern, where you have 3 layers:
presentation
logic
data
Two things confuse me around MVC:
"Model" component is often presented as the data layer above (database abstraction). But where does the "high-level" logic belong to? Like deciding what you will do with the data and how, checking permissions etc. Sometimes I've seen some of this in the controller, but it is really confusing for me to decide which belongs where.
The MVC pattern is presented as a circle of 3 components which send messages to each other, like M -> V, V -> C, C -> M, and the other way around. I understand perfectly the Three-tier design, where one layer calls the layer below itself, but not the other way around! The functions in your programming language can call other functions (you can call it "sending a message") - but it is an oriented tree graph. But how can the lower layer, or, how can the called function "send a message" or "notify" the calling function?
Maybe I am too much concerned by the MVC pattern and could happily stay with my Three-tier designing? Anyway, I would like to understand MVC pattern to at least see if it is worth using for me.
The model is another way of saying your domain knowledge, your controller should be deciding what models to display, update, create, and your view should just present the data the controller has decided to present. To address your second question the model is normally passed to the view through the controller to the view for the data to be presented.
For more details scroll down the the Model View Controller section of this page

Swing - Best Way to Interact between views?

In a Swing application, what is the best way to send data (interact) between two views?
Looking at the Coupling session in the Study Guide to the SCJP 6, it says:
All nontrivial OO applications are a mix of many classes and
interfaces working together. Ideally, all interactions between objects
in an OO system should use the APIs, in other words, the contracts, of
the objects' respective classes.
If I understood this correct, the better way would be create interfaces (contracts) to each view, and if needed use this interfaces methods to retrieve data. Is this a good way? Spending a good time creating a lot of interfaces to say what is exposed by a view is ok?
Another way that I think is to have classes to hold the data (Model) of a view. In this case, is a good approach access directly this model classes?
Thanks in advance.
The notion of a separable model pervades Swing, as outlined in A Swing Architecture Overview. Typically, each model is represented by an interface; a few include an AbstractXxxModel with some basic event plumbing; and many have a DefaultXxxModel with a standard implementation.
It completely depends on what design choice you are making. There are times where the design choice we will suggest is better for View's data sharing but it demolishes the other aspect of your software. So in order to balance you have make design choice in order to make your application run smoothly.
I personally prefer MVC design pattern. It works for me every time! read more about MVC on :
Model View Controller
Good luck!
Note : In MVC two views never interact with each other but rather they use controllers to get data from model and basically each view has controllers with a reference to it's data model.

Is this a correct way of using MVC in java?

I have a model with a reference to the view, a controller with references to the model and view, and a decoupled view, with no references to the above.
The controller has all the listeners of the view, and takes care of events by calling methods on the model and view. The model calls methods on the view in some of it's methods.
Is this bad for maintenance and reuse and should all view methods be called only in the controller? Also, i haven't set up the model as the observable and the controller and view(s) as observators, as seen in some examples. Is this common practice to do so?
I would avoid coupling directly from the model to the view, or at least only as very special circumstances (and even then, this might hint that the state being manipulated is not true model state, but presentation state.)
In general, the model uses listeners to notify of changes rather than a direct coupling to the view. The view can have a direct reference to the model, so that it can get the data to display. Although this may not be strictly necessary - all changes can be propagated from the model to the view as listener notifications, at the time the view needs it, but this is usually a bad idea, since it couples the rendering of the view with notifications from the model.
To sum up, the controller makes changes to the model, which notifies interested parties (including the view). The view has a reference to the model to retrieve data.
Following the classic MVC pattern, a model would not have direct access to a view. However, indirect access - such as via an observer - would be helpful to alert the view to changes in the underlying data.
The model should not know anything about the view.
The view should know how to populate itself given a model.
If you model is going to change it might want to make the view know of any changes so the display can change.
There are so many flavors of MVC, that i will decline to call any as "wrong".
See http://martinfowler.com/eaaDev/uiArchs.html
Look for one fitting flavor and go with it.
On the desktop Swing is a good GUI library. Check out this article to see how it relates to MVC. For the enterprise, take a look at Martin Fowler's Model View Controller pattern (330) for web-based applications. If you don't have his book (Patterns of Enterprise Application Architecture), I highly recommend getting hold of a copy.
There's a nice blog article at Sun.com: Java SE Application Design With MVC. It starts with explaining the "classic" MVC pattern where the view and model interacts directly with each other and then goes on with the newer "centralized controller" MVC pattern where the view and model can only communicate with each other through the controller (model is decoupled from view). It contains a Swing targeted example as well.

Categories