I want to practice MVC with java but at the first I want to create a simple console app and separate it into MVC I know I can separate MODEL from console(view + controller) but how can I separate view and controller in this situation?
-
Perhaps one way to think about how to separate it is to think of how you'd implement it on a network where the model is on one computer, the view on another, and the controller connecting the two. But yes, it can be done.– Hovercraft Full Of EelsNov 19, 2011 at 13:21
-
Still I need an advice how to separate view and controller in a console app ? any idea of how to do it?– Vahid HashemiNov 19, 2011 at 14:08
-
@austin powers: have you tried what I suggested?– TudorNov 19, 2011 at 14:13
-
I've read your answer but as far as I know controller should be responsible for interaction between model and view.Not to get any input from user directly . Correct me if I'm wrong.– Vahid HashemiNov 19, 2011 at 14:16
-
@austin powers: The controller is used to handle events coming from the user input. Like when you press a key in the console or enter some text. Then it informs the model or view to change accordingly.– TudorNov 19, 2011 at 14:17
3 Answers
Yes
The whole idea of MVC is that view doesn't matter. So, basically, you can build whatever you want application using MVC principles.
Well you can mimic that behavior by having a class that handles user console input (controller) and another class that deals with rendering the model on the console output (view).
It's not really what MVC is for, but if you really want to do it...
Edit: Ok, I'm going to give you some concrete ideas about the controller. You know that in a GUI app, you have the so-called "message loop". You will need to mimic this in your console app. Try to start a parallel thread that receives console input in an infinite loop. When input is received, call a handler method from the controller class.
Generally MVC architecture is purely for enterprise large scale application. First you need to understand your requirement and you are very clear that what you achieve after implementing this MVC in your java console.When You are implementing MVC in you small application you cant seen its advantage and its architecture . If you want to feel it you can select small web application just like simple login page and and do some processing.
If i am not correct please correct me.. Give me your valuable suggestion also ..........
Thanks
-
2Actually I don't agree. The advantages of separating model, view and controller are visible even for small applications. Your code is clearer and easier to understand. Mixing everything (GUI rendering, event handling and data access) in one class/module/program makes your code difficult to maintain.– MatteoNov 19, 2011 at 13:35
-
@Matteo...Thanks For correct me.... But my mean to say , that we cant clearly "feel" it in small console base application. Nov 19, 2011 at 13:39
-
1It's a great way to separate concerns logically for creation and testing, so that the model may be debugged and enhanced in this simpler console environment and then later attached to your more complex GUI or rich internet application view. It also forces you to use interfaces rather than classes and to use loose coupling -- both great benefits. Nov 19, 2011 at 13:57
-
2MVC came from GUI applications, from Smalltalk. The scale of the application has very little to do with it, nor the implementation mechanism (GUI, web, etc.) Nov 19, 2011 at 13:58
-
Matteo and Hovercraft, yes, it does make development easier to use MVC. I wrote a connect4 game for the console using MVC; I didn't have to rewrite everything when I ported it to Swing.– toto2Nov 19, 2011 at 15:50