MVC design question
I'm developing basic GUI application. I have model class which counts time, I need to display this time to label in specific format. What is the right way to do it in according to MVC paradigm ? Logically I think it should be formatted in the view, but view is standard label control and implementing sub-label class seems a little bit overwhelming task for this case. Would it be right if I format it in the controller ? Or maybe I should f开发者_如何学JAVAormat it in model?
I see presentation as the View's job. Formatting according to the user's locale and preferences is clearly a presentation issue. It may seem like a bit of an overhead, but surely you end up with a reusable widget, that can be used in many Views? Many existing View frameworks would provide such functionality out of the box.
Just to add more thing ... in my world the Controller doesn't even know what data is displayed by the view. A model might make various pieces of information available, a view might choose only some of them. A date might not be displayed at all, or be represented by a nice little Calendar widget. In either case the Controller does not need to format the date. Let the View do his job, and get the data from the Model or DTO returned by the Model and passed on without interpretation by the Controller.
If you want to test the formatted time, then include the formatting logic in the controller.
Otherwise, it's fine to leave it in the view.
I would put internationalization formatting in the controller, as I believe that views are intended to simply show what they're given. This also gives the benefit of the controller being able to choose completely different views for different locales, if your application ever needs that.
As per MVC pattern, here is what you might consider doing:
1: You run your model from the controller and get the count time 2: Controller gets the result from model and sends to view 3: You output the same variable on your view page.
That's it...
精彩评论