0

I am trying to create a graphical flashcards app from scratch. I have a few questions:

a. I have used Swing to build some apps in the past, calculator app. But I felt that was a trivial application so I want to ramp up my skills as a Java developer.

b. I have been told that a gold standard is to build a small application that uses one of these: MVC, MVM, MVVM and so on.And since I am learning design patterns, I was hoping to use it in the application.

c. My classes are such:

  • A model: Flashcard.java(It has a list of answers, a list of pictures, a question), A FlashCard Manager(to perform CRUD)
  • Different view classes: GUI interface
  • Controller: All the event listeners
  • App: To initialize the app

d. I have tried to read online examples of such a combination and what was proposed in for the Manager or DAO, was to have it connect to JDBC for the database. I am trying to simulate that by using a HashMap> for the same purpose.Is that correct or do I have to use JDBC or SQLite?

e. Also am I supposed to have a controller-view pair, that is for every JComponent that uses an event listener or a controller for windows( startup window, main application window, child windows)?

f. Also should the controller class be an interface for all these controllers?

I do not have code because I am still developing it but I just wanted to get a general idea. Thanks for answering my questions.

corax
  • 193
  • 1
  • 4
  • 17
  • (d) sounds reasonable, what you want to do is hide the underlying implementation, so if you decide to use a JDBC connection later on, you don't need to change all the code that relies on it – MadProgrammer Jul 14 '15 at 23:33
  • You're overthinking (e), not every `JComponent` needs to have it's own controller. In fact, you'd find this a complete pain as `JComponent`s are there own controllers. Instead, each logical "view" (which may be a container of a number of components) will need a controller. Between the view and the controller, there is a contract which describes the actions and functionality that the two can perform. When I do this kind of thing, I work on not exposing any of the UI components directly to any outside source – MadProgrammer Jul 14 '15 at 23:35
  • This means I tend to attach listeners to the components within the view and then use the view/controller contract to communicate via these listeners. This means if I want to change the way the view works, I don't need to recreate the contract, simply ensure I can support it – MadProgrammer Jul 14 '15 at 23:36
  • (f) is a yes. So should the model and view's. This way you are always working to the contract and not the implementation – MadProgrammer Jul 14 '15 at 23:36
  • The controller is the coordinator between the model and the view, it monitors events which are generated by these two and coordinates the required updates. Try not use the controller as a micro manager of either. Both the model and view should have their own internal works which then support the contract between themselves and the controller. For a detailed example, have a look [here](http://stackoverflow.com/questions/26517856/java-swing-where-do-actionlisteners-belong-according-to-mvc-pattern/26518274#26518274) – MadProgrammer Jul 14 '15 at 23:41

1 Answers1

1

b. I have been told that a gold standard is to build a small application that uses one of these: MVC, MVM, MVVM and so on.And since I am learning design patterns, I was hoping to use it in the application.

This both right and wrong. Some API's simply don't support the notion of a pure MVC (or variation). Swing for example implements a form of MVC of it's own which is more like M-VC, where the model is separate, but the view and controller are bound.

Think about a JButton. Do you ever apply a controller to it? You can add listeners to it, you can even add listeners directly to it's model, but it has it's own internal control mechanism, which generates events based on keyboard and mouse interaction.

You need to beware of when a MVC might not be suitable or where the amount of work to implement a pure MVC is not worth the effort.

Having said that, you can wrap Swing UI's in another MVC layer, but you need to think about it at a different level. Instead of each controller been considered a "view", you look at the container, which might contain multiple controls and see it as the "view", which a controller then manages.

d. I have tried to read online examples of such a combination and what was proposed in for the Manager or DAO, was to have it connect to JDBC for the database. I am trying to simulate that by using a HashMap> for the same purpose.Is that correct or do I have to use JDBC or SQLite?

This is not an unreasonable idea, however, you want to ensure that the public interface remains constant. This would allow you to change the implementation at a later date to use JDBC or a web service without having to rewrite the code that depends on it

e. Also am I supposed to have a controller-view pair, that is for every JComponent that uses an event listener or a controller for windows( startup window, main application window, child windows)?

This will depend. I tend to avoid exposing individual components, as this exposes them to unwanted modifications (you may not want a controller to be able to change the text of a button for example)

As a general rule of thumb, I try to make the relationship between the controller and the view as vanilla as possible, meaning that the controller shouldn't care about how the view is implemented, only that it upholds the contract between the it subscribes to.

This generally means I have a functional view, which is contracted to produce certain events/data and allows certain interaction (from the controller)

f. Also should the controller class be an interface for all these controllers?

IMHO, yes. This allows a view/controller/model to be implemented differently, but allows the rest of the code to continue working with it without the need to be modified.

The bottom line is, implementing a MVC over Swing is not as simple as people think it is. It takes some planning and consideration, but it is doable.

For a more detailed example, have a look at Java and GUI - Where do ActionListeners belong according to MVC pattern?

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks. I do not want to sound flaky or unmotivated but in the future, should I use JavaFx to implement such an app or a third-party libray or leave the langauge altogther? – corax Jul 15 '15 at 03:59
  • @corax That's up to you. I've not used JavaFX, so I can say. While MVC is not old per say, the push to use it (at all costs) kind of is. Is it important? Yes and no. You need to realise that a lot of APIs where written without it in mind (directly or indirectly) so some times, you might find it a issue to implement in the purest of senses – MadProgrammer Jul 15 '15 at 06:07