0

I'm developing a software project in java swing. I want to follow MVC design pattern for better maintainability, portability and reliability. I've looked upon MVC in Swing on internet and found some good results like THIS , THIS , THIS, THIS, THIS and few more (youtube too) and studied them. Then I built a design architecture for my project based on MVC design pattern. Here is the structure which simulate my actual requirements:

image

Here is little description about the architecture

  • Main Controller: It is responsible to initialize and to start the application. It initialize MainView and some other sub-controller like StudentController,TeacherController. It Contains some methods like getStudentView(), getTeacherView() which provide viewable components of Student TAB, Teacher TAB to MainView
  • Main View: It is the main view which is JFrame that contains JTabbedPane which uses MainController#getXXXView() to display view in its tabs. (Here XXX can be replaced by Student, Teacher or any other viewable component which is provided by Main Controller
  • Student Controller: It controls the StudentView and Student model and Student model interface. It initialize the Student View and provides this view to Main Controller which is further provide view to Main View
  • Student View: It is a JPanel which contains JTable to display the student information and some JButton like add Student, Edit, delete etc.
  • Student: It is Student model class that has name, rollNo etc fields to store student information.
  • Student Model Interface: It is service layer. It takes a Student argument and send/add it to Student Viewable Model and store into database using Student Database Model.
  • Student Viewable data Model: It is a TableModel that is used to define JTable in Student View
  • Student Database Model: Contains method to store/retrieve student information in/from database
  • Teacher Controller: Just like Student Controller except it controls the teacher related tasks.
  • Other Controller: Represents many Student Controller like controllers

NOTE Teacher Controller and other controllers have same hierarchy as that of Student Controller. These controllers access their models and view. For e.g. Teacher Controller have Teacher View, Teacher and Teacher Model Interface

Working of the project

  • Firstly Main Controller initialize the Main View and all the sub-controllers and start the application.

  • Student Controller implements ActionListener and act when there is an event in Student View. It is responsible to add, edit, delete student information. It takes information from Student View, wrap in Student object and pass it to Model Interface

  • Model interface then pass Student Object to Student Database Model which store/update/delete the data in database. If it is successful, then Model Interface update Student Viewable data model which is a Table Model that reflects changes in Student View

So my question is whether it is a good design for this kind of application and if it is not, then what should do I do. How can I refine the design for better maintainability, reliability portability. What are some recommendations

I think its a straight and rigid question but I can't do better.

Sachin Kumar
  • 781
  • 1
  • 9
  • 28
  • First, Swing is already a MVC (albeit a MV-C), trying to wrap another "pure" MVC on the top is going to make your life, complicated. When you're looking at you design, ask your self the simple question - how easy would it be to remove any one part and replace it with something else? If you find the answer to be anything other then easy, then your design is wrong – MadProgrammer Nov 06 '17 at 20:49
  • 2
    Why is the `Student` separated from the model? Technically, a controller managers one view and one model. Also, why are there two sub `Student` models? Neither the view nor the controller should care how the model is created, only that it adheres to a specific contract. Also, in more "pure" implementation of MVC, the view and the model should no nothing about each other and all interaction is done through the controller - this part I find hard to swallow, as it means duplicating a bunch of code, where adhering to a prescribed contract/interface would solve the issue :P – MadProgrammer Nov 06 '17 at 20:52
  • @MadProgrammer Student is model class that has simple fields and getter and setter methods. Student view has JTable and it requires TableModel to display the student information. Student view doesn't know about any of these models, but it only accept TableModel as an argument. This Table Model is defined by Student view data model class. I built model interface as if in future i don't want to see student info, then I can simply remove student view data model. – Sachin Kumar Nov 07 '17 at 08:37
  • @MadProgrammer I didn't get your point for duplicate code and separating Student from model. How can I minimize my design. It would be better if you provide an answer with pictures – Sachin Kumar Nov 07 '17 at 08:57
  • Part of the problem is, you're trying to apply a MVC around a MVC, Swing is already a MVC. To my mind, the `Student` model should provide simple information about the number of students and the ability to get them, it's then up to the view to present in what fashion it needs, albeit a `JTable`, `JList`, so other navigable form. In a "pure" MVC, where the view and model don't know about each other, the controller begins to duplicate functionality, in particular of the model, as the view requests information from the controller, which the controller forwards to the model and back again – MadProgrammer Nov 07 '17 at 09:10
  • Part of the problem is, you've provided "too" much information. We don't care how the `StudentModel` is created, only that it provides a guaranteed contract to which other aspects of the API can bind to, there is no need for `StudentDatabaseModel` or `StudentViewableDataModel`, as they are implementation details (and in my mind should probably be the same thing) – MadProgrammer Nov 07 '17 at 09:12
  • @MadProgrammer according to you, StudentDatabaseModel and StudentViewableModel should be same. But is it not good to separate these module because database should be unaware of how information is shown in GUI and vice versa. As Swing is already MVC, how can I make best use of Swing's MVC in such kind of requirements. – Sachin Kumar Nov 07 '17 at 09:33
  • @MadProgrammer If I don't define the StudentViewableDataModel which is model for JTable or JList, then where should I define Model for JTable, JList or any other view-model component. Should i write these code inside StudentView or somewhere else. – Sachin Kumar Nov 07 '17 at 09:35
  • IMHO - You shouldn't need to, the fact that you have a "model" of the data is the important accept, the fact that the view "happens" to need some kind of additional model to display you model is an implementation detail – MadProgrammer Nov 07 '17 at 10:27
  • @MadProgrammer Can you explain it little more, with the help of diagram if possible – Sachin Kumar Nov 07 '17 at 10:43
  • @MadProgrammer Do you have any clue.... If you have an answer.. please provide..... – Sachin Kumar Nov 11 '17 at 18:36

0 Answers0